Schoolboy error using = instead of -eq in a conditional statement – not always!

I had a conversation the other day about powershell and the schoolboy error of using the vb script construct of = instead of –eq in a powershell conditional statement and I said that there is definitely a use case where it’s correct but at the time couldn’t think of a case where that was true.

Today I was asked to help someone debug a very simple script to get some information out of AD using a list of user names.

The input filename is just a list of usernames, not all of which exist in AD so the script needed to handle this cleanly.

This is an example where using = is a valid construct.

if ( $user = get-qaduser $_ -IncludedProperties $includedAttributes -Connection $proxy -SearchRoot $searchRoot )

What happens is, if there is an AD user object returned then the conditional statement is true and at the same time $user is set to AD User object. If no user is returned then It’s $null and the else clause will run.

In the actual example there were a lot of attributes required and this meant using the -IncludedPorperties switch and a long select statement with all the output variables including some text formatting using @{name=””;expression={}}

To make the code cleaner I used input and output array variables. I’d done this before and it makes the code look a lot cleaner but I’d never tried adding text formatting into the array. Amazingly it works even when using a conditional statement like this @{name=’Employee ID’;Expression={if ( $_.’employeeID’ -ne $null ){ $_.’employeeID’ } else { $_.’employeeNumber’ }}} – you’d think that would fail as you instantiate the array once not every time in the pipeline but it works, try it yourself.

$searchRoot = "OU=User Accounts,DC=MyDomain,DC=com"
$inputFileName = 'c:\Temp\UserLogonNames.txt'
$outPutFileName = 'c:\Temp\OutputReportName.csv'

$includedAttributes = @(

$outPutAttributes =@(
@{name='Logon Name';expression='samaccountname'}
@{name='First Name';expression='givenName'}
@{name='Last Name';Expression='sn'}
@{name='Employee ID';Expression={if ( $_.'employeeID' -ne $null ){ $_.'employeeID' } else { $_.'employeeNumber' }}}
@{name='Department Name';expression='department'}

# The beauty of doing it this way is it’s easy to add and remove attributes later on by just managing the array variable
# and it makes the actual ‘payload’ command nice and short and it’s even easier to see the long list or attributes
# you are getting as a otherwise it looks like this

#Select @{name='Logon Name';expression='samaccountname'},@{name='First Name';expression='givenName'},@{name='Last Name';Expression='sn'},@{name='Employee ID';Expression={if ( $_.'employeeID' -ne $null ){ $_.'employeeID' } else { $_.'employeeNumber' }}},@{name='Department Name';expression='department'},,'DisplayName','AccountIsDisabled'

$nullUser = "" | Select 'samaccountname','givenName','sn','employeeID','department','DisplayName','AccountIsDisabled'
$nullUser.'givenName' = 'No AD Object Found'

Get-Content $inputFileName |
ForEach-object {
if ( $user = get-qaduser $_ -IncludedProperties $includedAttributes -Connection $proxy -SearchRoot $searchRoot ) {
} else {
$nullUser."samaccountname" = $_ ; $nullUser }
} |
select $outPutAttributes #|
# Export-csv $outPutFileName -NoTypeInformation -Encoding "UTF8"


2 thoughts on “Schoolboy error using = instead of -eq in a conditional statement – not always!

  1. This is great, I had no idea you could do this. I’ve just incorporated it into a modification of an old script

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.