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
Get-RealLogonDate -SAMAccountName schu
Get-RealLockoutState -SAMAccountName schu
Get-InstalledApplication -Computername schu2-pc
Test-IAIComputer -ComputerName schu2-pc,schu-pc,ahoggins2-pc -full
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 ','
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