Summary: Learn about using the Windows PowerShell ISE to edit scripts, and to modify the script execution policy in this step-by-step blog.
Hey, Scripting Guy! I have a problem. I was following your blog yesterday about typing long commands inside the Windows PowerShell ISE, and I saved my work because I did not want to lose what I was working on, and all of a sudden I could no longer do anything. Is this a bug?
—BW
Hello BW,
Microsoft Scripting Guy, Ed Wilson, is here. This week I have been working diligently on the events for the 2012 Scripting Games. The Scripting Wife and I have also been working on the first ever PowerShell Saturday in Columbus, Ohio. At this event, I will be presenting a number of “beginning with Windows PowerShell” type of presentations. There will also be presentations about using Windows PowerShell with Active Directory, SharePoint, and even Exchange Server. There will be several special guest presentations from a world class grouping of speakers. If you are anywhere near Columbus, Ohio, you do not want to miss this event. Seating is limited, and available slots are going quickly.
Anyway, BW, one of the skills you will need to know about for the 2012 Scripting Games is how to use a script editor. Because the Windows PowerShell ISE comes free, you may as well learn how to use it. If you feel that it is limited, keep in mind that it can be extended. I have talked quite a bit about using the Windows PowerShell ISE object model to extend capabilities, and I have written a number of add-ins to assist me in working with modules, replacing aliases in scripts, adding command snip-its, and more.
BW, after you save the code in the Windows PowerShell ISE, it becomes a script. When this happens, you run afoul of the script execution policy on your computer. In the image that follows, a simple Windows PowerShell script (now saved) does not execute because the script execution policy does not permit running scripts.
I have discussed the Windows PowerShell script execution policy on several occasions. In one blog, I helped the Scripting Wife set the script execution policy on her computer. On another occasion, posted an excerpt from my best selling Windows PowerShell 2.0 Best Practices book titled Why Would I Even Want to Create a Profile in Windows PowerShell?
Now, the network administrator via Group Policy might set the script execution policy, but it might also not be set. By default, Windows PowerShell disables the execution of scripts. This means you can use Windows PowerShell to run commands, but you cannot run a script. To set the script execution policy for all Windows PowerShell hosts and for all users of the computer requires administrator rights. But a normal user can modify the execution policy for the current Windows PowerShell host and for the current user by specifying the scope of CurrentUser. The command to permit running scripts for the current user is shown here.
Set-ExecutionPolicy -ExecutionPolicy remotesigned -Scope currentuser -Force
In the image that follows, the previous command runs to permit the execution of Windows PowerShell scripts. The change takes effect immediately, and it does not require a reboot or the closing and reopening the Windows PowerShell ISE.
It does not take too long before output seems to clutter the output pane. In general, when I am developing a script, I like to clear the output pane after each run of the script so I can easily identify any newly appearing error messages. There are two ways to clear the output pane. The first is to use the “windshield washer” icon that appears in the image that follows.
The other way to clear the output pane is to use the same command that works for the Windows PowerShell console—the Clear-Host command. The Clear-Host command has two aliases: clear and cls. I found these aliases by using the Get-Alias cmdlet as shown here.
PS C:\Users\ed> Get-Alias -Definition clear-host
CommandType Name Definition
----------- ---- ----------
Alias clear Clear-Host
Alias cls Clear-Host
To use the Clear-Host command (or the cls or clear alias for the command), type it in the command pane, as illustrated in the image that follows.
If I need to edit the script, there are Cut, Copy, and Paste commands available in the tool bar. Because the Windows PowerShell ISE is a standard Windows type of application, there are also the following shortcuts to assist you in quickly editing your script with the ISE.
Command |
Keyboard shortcut |
Copy |
Ctrl + c |
Cut |
Ctrl + x |
Paste |
Ctrl + v |
Undo |
Ctrl + z |
Redo |
Ctrl + y |
Find (in script) |
Ctrl + f |
Find Next (in script) |
F3 |
Find Previous (in script) |
Shift + F3 |
Replace (in script) |
Ctrl + H |
Go To Line |
Ctrl + G (then type line number) |
Select All |
Ctrl + A |
Not all of these commands are available as buttons on the tool bar. In fact, only the most common five commands appear on the tool bar. The remaining commands are available via the Edit menu (or you can use the keyboard shortcuts to avoid having to remove your hands from the keyboard to use the mouse). The five tool bar buttons are shown in the following image.
BW, that is all there is to using the Windows PowerShell ISE to edit scripts and to set the execution policy. ISE Week will continue tomorrow when I will talk about more cool things.
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