Category Archives: PS Tip

PS Tip: PowerShell Catches

In this example I want to check if a list of users samAccountNames returns a list of matching displaynames. In the event that the user is not found I don’t want a system error displayed, I want a custom error message. This is done using a Try & Catch statement, the Try element being the test and the Catch element what I want the custom error to be.

$users = get-content C:\team-members.txt
Foreach ($user in $users) {
try {
(get-aduser $user).Name
catch {“$user not found in AD!” #this text will replace the system error output


PS Tip: Is Hyper-Threading enabled on my computer

Open an elevated PS session and enter the following:

get-wmiobject -Class win32_processor | ft -Property NumberOfCores, NumberOfLogicalProcessors -auto

If Hyper-threading is enabled you’ll have twice the number of logical processors that you have of physical cores. In my case Hyper-threading is enabled due to the 4-8 ratio.



Disclaimer: provided “AS IS” with no warranties and confer no rights

PS Tip – Use local variable in Invoke-Command (remote computer)

I have a locally declared variable $var1

Using it directly doesn’t work, the value of the variable is not carried over within the invoke-command scriptblock.

$result2 = Invoke-Command -ComputerName $computer {(gwmi Win32_NetworkAdapter | where {$_.InterfaceIndex -eq $var1}).MACAddress}

Instead you have to use the “Using” scope modifier followed by a colon, thus $var1 becomes $using:var1

$result2 = Invoke-Command -ComputerName $computer {(gwmi Win32_NetworkAdapter | where {$_.InterfaceIndex -eq $using:var1}).MACAddress}