Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Create a Really Cool PowerShell ISE Profile

$
0
0

Summary: Learn how to create a powerful Windows PowerShell ISE profile by running a single script.

 

Microsoft Scripting Guy Ed Wilson here. One of the problems with writing a daily blog is that after a while, you end with a huge collection of articles and scripts. When I got a new laptop, and immediately left the country, I was really happy that I could access the Hey, Scripting Guy! Blog, and copy scripts so I could customize my Windows PowerShell ISE. Then I realized I had a problem: everything was cumulative, which meant that I spent hours and hours copying scripts, running scripts, and checking through dozens of Hey, Scripting Guy! Blog posts (many of which referenced other Hey, Scripting Guy! Blog posts). You get the idea.

Luckily, I was able to begin with the Weekend Scripter article, Clean Up Your PowerShell ISE Profile by Using a Module. But I wrote that article nearly a year ago, so there was a huge amount of new material to work through. In the end, I decided to do a “new release” of my profile and associated modules.

I have uploaded my current Windows PowerShell ISE profile to the Scripting Guys Script Repository. The profile script is very simple, and it is shown here:

import-module PowerShellISEModule

import-module MenuModule

import-module SnippetModule

import-module copymodule

BackUp-Profile

Add-MenuItems

New-ModuleDrives

The reason the code is so short is that everything is in the four accompanying modules. I will begin with the PowerShellISEModule. This is the main module, and it contains a number of really cool functions and a few aliases for those functions. The aliases and associated functions are discoverable by using the Get-Command cmdlet. Here is the code I use:

Get-Command -Module powershellisemodule | sort commandtype

The command and associated output are shown in the following figure.

Image of command and associated output

The functions from the PowerISEModule are shown here. I have included links to the Hey, Scripting Guy! Blog post where I discussed each of these functions:

New-ModuleDrives                                                                               

Start-iseTranscript                                                                            

Remove-AliasFromScript                                                                         

Set-Profile                                                                                    

Add-Help                                                                                       

Add-HeaderToScript                                                                             

backUp-Profile                                                                                 

Get-logNameFromDate                                                                            

Get-FileSystemDrives    

 

The MenuModule is new, but it incorporates all of the elements from my articles that talk about adding menu items to the Windows PowerShell ISE. The four functions contained in this module are shown here:

Add-MenuItems                                                                                 

Get-Fonts                                                                                     

Get-PsIseColorValues                                                                           

Set-PsISE   

I made a few changes to the Add-MenuItems function because I moved the ISE preference scripts into functions in order to consolidate the functionality and to make it easier to customize the ISE. Now instead of having numerous dependencies on various scripts that may reside in different locations, I put everything into a module.

The SnippetModule is also new. I modified the Export-ModuleMember command to export only one function. When using the Get-Command cmdlet, only one function appears. That function is the Get-CodeSnippetV2 function. I wrote a series of articles that talk about adding code snippet functionality to the Windows PowerShell ISE.

However, there are many other functions in the SnippetModule:

New-Sniptype

Remove-sniptype

copy-codeSnippetsFromInterNet

Test-IsAdministrator

Register-CodeSnippets

The preceding functions are used to install the code snippet functionality into the Windows PowerShell ISE. If you need to do this, modify the Export-ModuleMember command at the bottom of the snippetModule. This command is shown here:

Export-ModuleMember -Function Get-CodeSnippetV2, Register-CodeSnippets -Variable sniphome

After you have installed the code snippets, there is no reason to include the Register-CodeSnippets function into your daily Windows PowerShell ISE profile. I highly recommend that you use the –path parameter and specify the path to the snip.zip file when installing the code snippets feature. When I was testing the installation from the Internet feature, it worked once out of four attempts. The command to register the code snippets using the path feature would appear something like this (assuming I had copied the snip.zip file to the c:\fso folder):

Register-CodeSnippets –path c:\fso\snip.zip

I initially wrote the Copy-Modules.ps1 script for my Windows PowerShell 2.0 Best Practices book, and I have been using it on a regular basis ever since. Well, today I decided to add it to a module itself. The reason is I wanted to make it easy to bring into my Windows PowerShell ISE profile. I put all of the functions into the CopyModule module, and only export the Copy-Modules function. The Copy-Modules function takes a single parameter that is the folder that contains all of the psm1 files to import. This command is shown here:

Copy-Modules –path c:\fso

To install this ISE module release, use the Copy-ISEProfile.ps1 script that is included in the ISE_Modules.zip file that you can download from the Scripting Guys Script Repository.

The Copy-ISEProfile.ps1 script is shown here:

$path = "C:\fso"

If(!(Test-Path $profile))

  {new-item -Path $profile -ItemType file -force}

Import-Module -Name (Join-Path -Path $path -ChildPath copymodule.psm1)

Copy-Modules -path $path

psedit $profile

When you run the Copy-ISEProfile.ps1 script, provide the path to where the ISE modules are extracted. At the end of running the Copy-ISEProfile script, it will open the ISE profile in a new tab. Paste the ISE module commands to the profile. These commands appear here, but are on the Scripting Guys Script Repository as well:

import-module PowerShellISEModule

import-module MenuModule

import-module SnippetModule

import-module copymodule

BackUp-Profile

Add-MenuItems

New-ModuleDrives

 

Well, that is about it for now. Join me tomorrow for more Windows PowerShell goodness.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

 


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>