PowerShell Scripts with WhatIf

Sometimes you want to simulate the execution of a PowerShell script. Instead of actually doing the things defined in the script you just want to see what would have been done. Many Cmdlets understand the parameter -WhatIf. If you use it e.g. when creating a directory it actually will not be created. Instead you get a message describing what would have been done:

1
New-Item -ItemType Directory -Name "test" -Path "c:\temp" -WhatIf
PS C:\> New-Item -ItemType Directory -Name "test" -Path "c:\temp" -WhatIf
What if: Performing the operation "Create Directory" on target "Destination: C:\temp\test".
Read more gblog_arrow_right

Passwords in PowerShell

Sometimes you have to use passwords in PowerShell. For example if you want to shut down a remote computer and have to authorize with another user than the current. You can prompt for the username and password while running the script with Get-Credential:

1
2
$Credential = Get-Credential
Stop-Computer -Computer 192.168.2.10 -Credential $Credential -WhatIf
Read more gblog_arrow_right

Validate PowerShell parameters

When you use parameters in your PowerShell script you might want to check the values for valid data. Of course you can do these checks with some if statements. But you can also specify valid values when defining the parameters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# definition of parameters
Param(
    [Parameter(Mandatory=$True)]
    [string]$StringParameter1,

    [Parameter(Mandatory=$True)]
    [ValidateSet("String1","String2")]
    [string]$StringParameter2,

    [Parameter(Mandatory=$True)]
    [int]$IntegerParameter1,

    [Parameter(Mandatory=$True)]
    [ValidateRange(0, 5)]
    [int]$IntegerParameter2
)

# own validation of parameters
if (-not($StringParameter1 -eq "String1" -or $StringParameter1 -eq "String2")) {
    Write-Output "Error! Valid values for StringParameter1 are ""String1"" and ""String2""."
    exit
}
if ($IntegerParameter1 -lt 0 -or $IntegerParameter1 -gt 5) {
    Write-Output "Error! Value of IntegerParameter1 must be in range from 0 to 5."
    exit
}

# output of the parameter values
Write-Output "StringParameter1: $StringParameter1"
Write-Output "StringParameter2: $StringParameter2"
Write-Output "IntegerParameter1: $IntegerParameter1"
Write-Output "IntegerParameter2: $IntegerParameter2"
Read more gblog_arrow_right

Add parameters to PowerShell Script

If you want to add parameters to your PowerShell Script you have to define them. In this defintion you also specify the data type. A simple example to get the idea:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# definition of parameters
Param(
    [string]$StringParameter,
    [bool]$BooleanParameter = $False,
    [int]$IntegerParameter = 10,
    [switch]$SwitchParameter
)

# output of the parameter values
Write-host "StringParameter: $StringParameter"
Write-host "BooleanParameter: $BooleanParameter"
Write-host "IntegerParameter: $IntegerParameter"
Write-host "SwitchParameter: $SwitchParameter"
Read more gblog_arrow_right