How to get the SID of a Security Principal with Powershell

This Powershell code snippet easily displays the SID of a Windows Security Principal:

1
2
3
$principal = New-Object System.Security.Principal.NTAccount("builtin\Administrators")
$principalSID = $principal.Translate([System.Security.Principal.SecurityIdentifier]).Value 
$principalSID

How to Copy Files from a Hyper-V VM to the Host

Sometimes you might need to copy files from a Hyper-V Virtual Machine or Guest to the Hyper-V Host.

PowerShell can easily help you with that. Just use the -VMName parameter with the New-PSSession cmdlet on the Hyper-V Host to create a new session to the Hyper-V VM and store it in a variable. Provide credentials for the VM if needed.

1
$session = New-PSSession -VMName "vmname" -Credential (Get-Credential)

Then use the stored session with the -FromSession parameter with the Copy-Item cmdlet and copy your file(s) from the VM to the Host.

1
Copy-Item -FromSession $session -Path "C:\path_on_vm\*" -Destination "C:\path_on_host\"

Using -ToSession gives you a solutions to copy files the other way round: from the Host to the VM.

PowerShell Performance: += Operator versus ArrayList to add Elements to an Array

Sometimes you need to add elements to an array in PowerShell within a loop without knowing beforehand how many elements you are going to add.

In the most cases using the += operator make it all work easily.
Since the += operator actually…

  • …creates a new array in the memory with a length of the current array +1
  • copies all the elements of the current array into the new array
  • adds the new element at the end of the new array
  • deletes the current array from memory…

…the whole process is extremely time consuming (or expensive) if your array grows larger and larger.
On the other hand, using the .Add() function at a System.Collections.ArrayList just adds a new element at the end of the array without copying it first, which is much faster.

Therefore, I would suggest using the .NET System.Collections.ArrayList instead as shown below.

To demonstrate the massive difference in performance between the += operator and the System.Collections.ArrayList, I wrote two short scripts to demonstrates the effect.
Read More

PowerShell here-string a handy multiline string value

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 { "$_" }