PowerShell here-string a handy multiline string

A here-string in PowerShell is a multiline string value which keeps its formatting, works without the need of escaping doubble ( ” ) and single ( ‘ ) quotes and can be used with variables as you know it.

This short example shows you how to use a here-string within PowerShell:

1
2
3
4
5
6
7
8
9
10
11
$myVar = "string value"
 
$hereString = @"
My
    formatted
        here-string
    with a variable $myVar
and some quotes " ' " ' "
"@
 
$hereString

You can even use a here-string to add C# code to your PowerShell script as shown below:

1
2
3
4
5
6
7
8
9
10
11
12
13
Add-Type -TypeDefinition @"
    public class DoSomeMath {
        public static int Addition(int n1, int n2) {
            return n1 + n2;
        }
        public static int Subtraction(int n1, int n2) {
            return n1 - n2;
        }
    }
"@
 
[DoSomeMath]::Addition(5,2)
[DoSomeMath]::Subtraction(5,2)

PowerShell: how to Split an Array into smaller Arrays (Chunks)?

Splitting an array in PowerShell into smaller arrays / chunks can come quite handy from time to time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Creating a new array
$inArray = @(1..20)
 
# Defining the chunk size
$chunkSize = 3
$outArray = @()
$parts = [math]::Ceiling($inArray.Length / $chunkSize)
 
# Splitting the array to chunks of the same size
for($i=0; $i -le $parts; $i++){
    $start = $i*$chunkSize
    $end = (($i+1)*$chunkSize)-1
    $outArray += ,@($inArray[$start..$end])
}
 
# Printing the output array in console
$outArray | ForEach-Object { "$_" }

PowerShell Function: make a Parameter dependent of the Value of another Parameter

I had the case where I needed to make sure that one switch parameter of a PowerShell function can only be used, if another parameter had a certain value.
For example: the switch parameter -MYSWITCH can only be used if the value of the parameter -Selection is set to “POSSIBLE”.

The following PowerShell function is written to do exactly that.

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
function myFunction() {
 
    [cmdletbinding(
        DefaultParameterSetName='Default'
    )]
    Param
    (
        [Parameter()]
        [String]$Selection,
 
        [Parameter(ParameterSetName='Selection')]
        [ValidateScript({ $Selection -eq 'POSSIBLE' })]
        [Switch]$MYSWITCH
    )
 
    # Print out current ParameterSet Selection
    $PSCmdlet.ParameterSetName
}
 
# Selection is POSSIBLE and therefore, the -MYSWITCH switch is allowed
myFunction -Selection POSSIBLE -MYSWITCH 
 
# Selection is IMPOSSIBLE and therefore, the -MYSWITCH switch is NOT allowed => throws an error
myFunction -Selection IMPOSSIBLE -MYSWITCH
 
# Selection is IMPOSSIBLE and therefore, the -MYSWITCH switch is NOT allowed => works well because -MYSWITCH is not used
myFunction -Selection IMPOSSIBLE

If the -Selection is set to anything different then “POSSIBLE”, using the switch -MYSWITCH just throws an error. This is the expected behavior in that case.