Select Page

So there you are, enjoying a cup of joe with R2D2 when the request comes to you to get data of some sort for somebody for reasons unknown. Usually this person is in a supervisory role and they want information like “Hey bro, can you get me a list of users (with their titles) in AD who are humans, part of the Empire, and enabled? K thanks.” And of course, you ponder to yourself why your overlord needs that particular list of users, but never the less, you comply with the demand and turn to PowerShell and start slapping your keyboard to get the info. Before we can manipulate objects in PowerShell, let’s run a basic cmdlet to store as an object like:

Get-ADUser -Filter "enabled -eq $true" -Properties Allegiance, Species, Title | Where {$_.allegiance -eq 'Empire' -AND $_.species -eq 'Human' -AND $_.title -eq 'General'}

Wrong keyboard, but in our scenario, we found 8 users in The Empire after we slapped our keyboard a few times. We’re able to tackle this type of request pretty quickly as long as the data is already there in AD. But what about adding a custom column to the 8 users we found with additional information that isn’t in AD?

Oh, you would just export to CSV and then add it in Excel? Touché…but, there is a more excellent way! 8 users, easy enough, but what about multiple columns and hundreds of users? What if we needed to use the data in a script?? Would you still want to do THAT in Excel???

You may have answered “yes,” but have you ever wondered, deep down inside, “How can I do this in PS instead?” Probably not, but I’m going to show you anyway since you’re already here and this post is like a car accident on the other side of the freeway that somehow causes traffic for your side because we just can’t help ourselves and people are nosy…

\\\Store In A Variable

Alright, let’s get to it! First, let’s store our user results to a variable, very thoughtfully named: $Users

$Users = Get-ADUser -Filter "enabled -eq $true" -Properties Allegiance, Species, Title | Where {$_.allegiance -eq 'Empire' -AND $_.species -eq 'Human' -AND $_.title -eq 'General'}

Great! Let’s look at what we have stored to make sure we know what we’re working with by typing $Users in the shell:

Beautiful. Some of my favorite people in the Empire…

\\\How Do We Add Stuff?

But how do we add stuff and you know, do other magical things? Manipulation is a strong word, but how do we manipulate objects in PowerShell? Well, the Add-Member cmdlet is used to add properties to a object, so we could try this:

$Users | Add-Member -MemberType NoteProperty -Name "Send Email" -Value "Yes"

And if we want to check what we just did to $Users, we call the variable again and for ease of reading, we pipe it to Select (alias for Select-Object) and choose the name and the new “Send Email” property we just added:

$Users | Select Name, 'Send Email'

That’s great…if we wanted to add the same value for each user.

\\\Magic +1

But what if we want to add different values for specific users? What if we wanted to only send emails to users who have the title of “General” and not to the others? So we want the same list of users, but the “Send Email” column needs to have a mix of “Yes” and “No” based on their title. As you well know (or will soon discover), there is more than one way of digging for money…here’s one of them. Well, actually, this isn’t a way to dig for money at all but it may save you 15% or more on your car insurance

Foreach ($U in $Users){
     if ($U.Title -eq 'General'){
          $Value = "Yes"
     }
     else{
          $Value = "No"
     }
     $U | Add-Member -MemberType NoteProperty -Name "Send Email" -Value $Value -Force
}

We’re going to loop through each user stored in $Users. We’re calling each “row” $U. So $U would be one user and his/her attributes.

Foreach ($U in $Users){
}

Then, in the ForEach loop, we’re determining the $Value that we’re going to give to the “Send Email” property by using an if/else statement. If the title of the individual user we’re looping through ($U.title) is “General” then $Value is going to be “Yes”. If the title is anything else, then $Value is going to be “No”.

if ($U.Title -eq 'General'){
     $Value = "Yes"
}
else{
     $Value = "No"
}

Finally, we are going to use the Add-Member piped from $U to add the “Send Email” property with the corresponding $Value based on the results from the if/else statement.

$U | Add-Member -MemberType NoteProperty -Name "Send Email" -Value $Value -Force

And wash, rinse, and repeat through each user in $Users.

Finally, once we run our ForEach loop, we can call our $Users variable again and pipe it out. We can use select to see if the “Send Email” value is different based on their Title:

$Users | Select Name, Title, 'Send Email'

Fantastic! We can relay this data in several ways and we now know how a few ways to manipulate objects in PowerShell. Specifically we used the Add-Member cmdlet in a ForEach loop to add an additional column. We’ve leveled up our ability to manipulate objects in PowerShell and we’ve successfully evaded getting forced choked today! What more could we ask for? Enjoy that cup of joe you rascal you!

OTHER POSTS YOU WANT TO READ

Spice Up HTML Emails with PowerShell – Part III

So far in this series we've fumbled our way around the kitchen and tried to wing it when sending HTML emails with PowerShell. It was clunky to say the least. We then went through our spice rack and built an HTML template, highlighting the nuances of each spicy element...

Spice up HTML Emails with PowerShell – Part II

In Part I of our scrumptious concoction we put our script into the oven to let it bake. But we forgot to add our secret sauce that's sure to leave our recipients drooling, which is clearly our goal. In this post we'll continue to spice up HTML emails with PowerShell...

Spice Up HTML Emails with PowerShell – Part I

I live in the South, specifically in God's country AKA TEXAS BABAY! There's plenty of amazing things about Texas and definitely some reasons to loathe being from Houston, but it's hard to knock our food. I mean, is there really even a debate between Whataburger vs...

Disable O365 Apps in License (PowerShell)

If it's free, it's better. This is basically my mantra for life and a really easy one to follow at that. You can apply it to all sorts of things: ice cream, candy, food. Okay, maybe just free food tastes better when you know you didn't have to pay for it. Especially...

Font Installer Script (PowerShell)

Things have smells. I know, this is a revolutionary idea and you may need to stop right here and take a breather while you process this massive revelation. Things that are brand new have a particular smell that vanishes shortly after hitting your nostrils. It's...
%d bloggers like this: