Categories

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.

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:

1

2

Send-MailMessage-From you@domain.com-Toitops@domain.com`

-Subject"Hello world"-SmtpServer smtp.domain.com

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:

1

2

3

4

$PSDefaultParameterValues=@{

"Send-MailMessage:From"="you@domain.com"

"Send-MailMessage:SmtpServer"="smtp.domain.com"

}

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

1

Send-MailMessage-Toitops@domain.com-Subject"Hello world"

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

1

2

$cred=Get-Credential

$PSDefaultParameterValues+=@{"*:Credential"=$cred}

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:

1

2

3

4

5

6

7

8

9

10

11

12

$jobtrigger=New-JobTrigger-Once-At(Get-Date).AddMinutes(1)

Register-ScheduledJob-Name"PowerShell Scheduled Job Example"`

-Trigger$jobtrigger-ScriptBlock{

Import-Module ActiveDirectory

$lockedaccounts=Search-ADAccount-LockedOut

if($lockedaccounts){

$body=$lockedaccounts|Select-ObjectName|ConvertTo-Html|Out-String

Send-MailMessage-From you@domain.com-Toitops@domain.com`

-Subject"Locked accounts"-Body$body-BodyAsHtml`

-SmtpServer smtp.domain.com

}

}

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:

Like this:

About the author: Jan Egil Ring

Jan Egil works as a Lead Architect at Crayon, Norway.
He mainly works with automation, and has a strong passion for PowerShell. He has been working with Microsoft infrastructure products such as Windows Server & System Center since the early 2000s. In the recent years the focus has been more and more related to cloud technologies in Microsoft Azure and hybrid environments.
He is a multiple-year recipient of Microsoft Most Valuable Professional Award for his contributions in the Windows PowerShell and Cloud & Datacenter Management technical communities.
He speaks regularly at user groups and conferences, such as Nordic Infrastructure Conference (NIC), PowerShell Conference Europe and PowerShell Summit.
He is a co-organizer of Azure User Group Norway as well as the MTUG (Microsoft Technology User Group) Script Club which focuses mainly on PowerShell.
You can follow him on Twitter @JanEgilRing.