1

Jan Egil Ring’s Favorite PowerShell Tips & Tricks

Disclaimer: This article is written based on the PowerShell 3.0 version available in the Windows 8 Consumer Preview. Features described may change in the final version of the product.

Feature #1 – Enhancements to the PowerShell ISE

I have always used the PowerShell Integrated Scripting Environment (ISE), but not as my primary script editor due to lack of some essential features, like brace matching. This is a very useful feature when working with scripts that contains several loops.

A major change to the ISE is the combining of the input and output panes which exists in PowerShell ISE 2.0:

In PowerShell ISE 3.0 there are 2 panes:

The default background color is also changed to be the same as in the PowerShell console (powershell.exe), which I think makes it look and feel more like PowerShell for people opening the ISE for the first time.

Of course, there are different opinions about this change, as some people do like the input/output panes in PowerShell ISE 2.0, but for me this is a welcome change.

Another useful feature I want to highlight is the snippets functionality. Imagine that you are in the middle of writing a script, and you cannot remember the syntax for a switch statement. With the new snippets feature you can hit Ctrl + J to launch the snippets menu, and then type “s” to get to the switch statement:

Then hit Enter and you will get a sample switch statement to use as a basis:

There are too many features in the ISE to cover them all in this article, but enhanced and vastly improved ISE is definitely one of my favorite features in PowerShell 3.0.

You can read more about new features in PowerShell ISE in the TechNet article What’s new in the Windows PowerShell ISE. I would also recommend you to read the “Windows PowerShell ISE.pdf” document released with the Windows Management Framework 3.0 – Community Technology Preview (CTP) #2.

Feature #2 – Default parameter values

Another exciting new feature is the $PSDefaultParameterValues preference variable. (For more information about this feature, see about_Parameters_Default_Values.) This is best explained with a few examples. Let us say that you want to use the Send-MailMessage cmdlet:

If this is a cmdlet you use a lot you will notice that there are some parameters you must specify which is mostly static, in this example the –From and –SmtpServer parameters.

You can define the default parameter values for these parameters in a special type of hash table like this:

Now the usage of the Send-MailMessage can be shortened to the following:

Another practical example is the –Credential parameter which can allow you to run all cmdlets that accepts the parameter using the specified alternate credentials:

When defining the $PSDefaultParameterValues preference variable in your PowerShell profile, the default values you specify will be available in every PowerShell session you open. And if you would like to temporarily turn off the feature, you can disable it like this:

$PSDefaultParameterValues[“Disabled”] = $true

Feature #3 – Job Scheduling

In previous versions there was no native way in PowerShell to work with scheduled jobs, except from a scheduled tasks module available in the PowerShell Pack which was part of the Windows 7 Resource Kit. Typically you would create a scheduled task which runs powershell.exe, and pass the –Command or –File parameters as arguments to execute your command or script.

In PowerShell 3.0 there is a new module called PSScheduledJob which have the following cmdlets:

The scheduled jobs created using the module can be found in the Windows Task Scheduler under Task Scheduler Library\Microsoft\Windows\PowerShell\ScheduledJobs.

A sample job:

Note: The sample requires that the Microsoft Active Directory module is available on the computer the job is scheduled to run on.

Here we can see that the job is created in the Windows Task Scheduler:

You can read more about the new job scheduling cmdlets in the Scheduling Background Jobs in Windows PowerShell 3.0 article on the Windows PowerShell Team’s blog.

Filed in: Columns, Tips and Tricks Tags: , ,

One Response to "Jan Egil Ring’s Favorite PowerShell Tips & Tricks"

  1. Paul Jensen says:

    I was searching internet for answer to my problem and came at your comments, perhaps you have such an answer – could you help me please?

    I use windows 8 with powershell 3.

    Q1. task scheduler does not wake PC from sleep.

    Q2. So I tried powershell 3 to do a scheduled job. I thought it was the program I call in the script, but when I call even Notepad it does the same. When I work in powershell and call the script from its console it works OK and Notepad loads with normal priority and windows handle. When I call the same script from a scheduled job it loads the Notepad and it exists as an instance but it does not open its window, does not have windows handle, the instance exists below normal priority, and there is no error not in script and not in event log.

    The notepad call command in the script is preceded and followed by sample scheduled job cmdlets and 10s waits to see that powershell works normally, only Notepad does not start properly.

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: