Skip to content

Instantly share code, notes, and snippets.

@Chirishman
Last active March 15, 2021 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chirishman/53365de7558cc18e2d4322065254ed3c to your computer and use it in GitHub Desktop.
Save Chirishman/53365de7558cc18e2d4322065254ed3c to your computer and use it in GitHub Desktop.
Tips And Tricks

Tips and Tricks

To evaluate something and also store it in a variable in the most concise way possible

if (($a = $true)){
    #TrueCondition
}

The extra set of parenthesis causes the statement to return it's value as well as storing it in the variable $a


To check for latest logon date in all Domain Controllers

Get-RealLogonDate -SAMAccountName schu

To check for an AD lockout in all Domain Controllers

Get-RealLockoutState -SAMAccountName schu

To check for a specific installed application on a remote machine

Get-InstalledApplication -Computername schu2-pc

To batch-ping servers

Test-IAIComputer -ComputerName schu2-pc,schu-pc,ahoggins2-pc -full

To join the entries in an array together in the most concise way possible

If you don't need a delimiter between the entries

-join(0,1,2,3,4)

If you do need a delimiter (such as a comma) the long way is:

@(0,1,2,3,4) -join ','

More on PowerShell Operators


To measure the performance of your code

To measure a single command or section of code when developing use Measure-Command to quickly get some performance information

$PerformanceResults = Measure-Command -Expression {
    Start-Sleep -Seconds 15
}

$PerformanceResults

To measure specific sections of code in a production script leverage dotnet stopwatch objects which are much more flexible. They can be stopped, started, and cleared at will.

Function Test-FunctionPerformance {
    [CmdletBinding()]
    Param()
    $Overall = [system.diagnostics.stopwatch]::startNew()
    $OddSteps = [system.diagnostics.stopwatch]::startNew()
    Start-Sleep -Seconds 3
    $OddSteps.Stop()
    Write-Verbose -Message "Step 1 Took $($Overall.Elapsed)"
    
    $Step2 =  [system.diagnostics.stopwatch]::startNew()
    Start-Sleep -Seconds 5
    $Step2.Stop()
    Write-Verbose -Message "Step 2 Took $($Step2.Elapsed)"
    
    $Step3 =  [system.diagnostics.stopwatch]::startNew()
    $OddSteps.Start()
    Start-Sleep -Seconds 10
    $Step3.Stop()
    $OddSteps.Stop()
    $Overall.Stop()
    Write-Verbose -Message "Step 3 Took $($Step3.Elapsed)"
    
    Write-Verbose -Message "Odd Numbered Steps Took $($OddSteps.Elapsed)"

    Write-Verbose -Message "Entire Function Took $($Overall.Elapsed)"
}

Test-FunctionPerformance -Verbose

Stopwatch objects are also especially good for measuring loops

$Overall = [system.diagnostics.stopwatch]::startNew()
$ThisLoop = [system.diagnostics.stopwatch]::New()
$IndividualLoopPerformance = [System.Collections.ArrayList]::new()
1..100 | %{
    $ThisLoop.Restart()
    Start-Sleep -Milliseconds $_
    [void]$IndividualLoopPerformance.Add($ThisLoop.Elapsed)
}
$ThisLoop.Stop()
$Overall.Stop()

Write-Verbose -Message "Loop Performance Metrics `r`n$(($IndividualLoopPerformance | measure -Average -Property totalseconds -Maximum -Minimum -Sum | ft ) | out-string)" -Verbose

Write-Verbose -Message "Entire Loop Took $($Overall.Elapsed)" -Verbose

The output of which will look like

VERBOSE: Loop Performance Metrics 

Count     Average       Sum   Maximum  Minimum Property    
-----     -------       ---   -------  ------- --------    
  100 0.057919116 5.7919116 0.1153261 0.006223 TotalSeconds



VERBOSE: Entire Loop Took 00:00:05.8833751

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment