Super Simple Script V

DISCLAIMER: This was originally a way to document what I was learning when I first ran into PowerShell as a student who had no prior scripting experience. The examples given and the posts done in relation to the project most likely do not follow best practices and should definitely be reviewed if they are going to be used in any fashion. You have been warned! 🙂

Now that my hectic semester is over — time for some simple scripts that do some powerful stuff!

For this SSS, with just a few lines, we are able to receive the email addresses of everyone within an AD user group. I whipped up this script when I needed to retrieve the names and emails of everybody in a specific group that were accessing an application in a Citrix XenApp environment:

# Bring on the AD cmdlets
Import-Module activedirectory
# Prompt the user for a group to extract info from
$userlist = Read-Host “What ADGroup would you like to extract users/email-addresses from?”
# Gather user objects from $userlist
$users = Get-ADGroupMember $userlist
# Foreach user object in $users 
Foreach ($user in $users) {
        (Get-ADUser $user).Name + ” ” + (Get-ADUser $user -Properties mail).mail >> emailList.txt
# Open notepad with the results at script end

notepad emailList.txt

Everything should be, for the most part, straight-forward. That is, other than what is going on in the Foreach statement. What’s with the parentheses being wrapped around certain commands? What about the “+” and empty quotes?

(Get-ADUser $user).Name

This uses Get-ADUser to retrieve user information from the current user, but only is grabbing the Name property. We are then adding a space, for formatting purposes

+ ” ” +

Before we grab the next user property, being their email!

(Get-ADUser $user -Properties mail).mail

This should output something similar to this in the notepad document that eventually opens up at the end:


Awesome stuff. Keep in mind, this script only has one prompt, and it includes no kind of error checking or error-logging. You WILL see red if this script finds groups as members of the group, since you can’t grab ADUser information or an email from them. Other than that, all users should still be pulled. This is just a simple mock-up to query information in just a few lines!

Stop by next time for more awesome, where I will go over my first Linux Bash script that allows you to query local user information and format the output nicely with awk.

Leave a Reply

Please log in using one of these methods to post your comment: 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