More Tips for Better PowerShell
Let’s continue our look at ways to take PowerShell code from good to better. I’m basing my talking points on a code review I did on a friend’s GitHub repository. He has a lot of good things in his code, and the issues I’m bringing up are not unique to his work. I have seen similar code from other people over the years. Again, it isn’t that the code doesn’t work, but rather that I think it can be better.
Switch to Switch
Here is a typical snippet of PowerShell code.
$os = Get-CimInstance Win32_OperatingSystem
if ($os.BuildNumber -eq 22631) {
Write-Host "Running Windows 11 code"
}
elseif ($os.BuildNumber -eq 19044) {
Write-Host "Running Windows 10 code"
}
elseif ($os.BuildNumber -eq 14393) {
Write-Host "Running Windows Server 2016 code"
}
elseif ($os.BuildNumber -eq 17763) {
Write-Host "Running Windows Server 2019 code"
}
Want to read the full issue?