Creating an array using Select-Object with the ‘Switch’ option

A sample scenario:

We start off with an array of users that we get from Active Directory and we need to correlate the data with another array that contains time sheet information about the users. The first array is $users – and we get this by running

Here is the output from the $users array

namemailgivennamesurnameemployeeid
probertsproberts@domain.compaulroberts12345
jdavisjdavis@domain.comjohndavis12347
dsuttondsutton@domain.comdavidsutton12352
ssmithssmith@domain.comsusansmith12355
jmannjmann@domain.comjoanmann12357
mreynoldsmreynolds@domain.commaryreynolds12362
pjonespjones@domain.compaulajones12365
mrussellmrussell@domain.commikerussell12367
pdunnepdunne@domain.competerdunne12372
pwardpward@domain.compaulward12375
ssummersssummers@domain.comsamsummers12377
fwilliamsfwilliams@domain.comfredwilliams12382
mwhitemwhite@domain.commonicawhite12385

 

And we have a spreadsheet (in CSV format) with user’s hours, rate of pay and employee numbers like below.

empnohoursrate
1234537.512
1234737.512
123524012
1235537.515
1235737.512
123624012
1236537.515
1236737.515
1237237.525
123755015
1237737.515
1238237.515
1238537.515

 

We can import this CSV by using

What we want to output is an array with the user’s name, employee number, hours worked, rate of pay and the total gross pay. (This is only a sample script so you can get the idea of how it will work)

Using the command below we can generate a new array called $payroll with all the required data from both arrays

 

There is much more that can be achieved using the select-object and I will cover these in the next post

As always, have fun playing around with this and hopefully it helps you out in your scripting and feel free to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *