One of my first PowerShell scripts.

 In my current role I have been afforded the opportunity to learn and expand my knowledge of Powershell.  I've been reading books and watching videos to get the basics down.  The most helpful thing that I have found is to actually have a project to work on and to spend lots of time googling.  

My first Powershell script came about due to a ticket that had sat in the que for quite some time.  The ticket requested to have location and organizational (tile/department) information displayed in Exchange.  I first had to contact HR to get a list of all employees with their department, and job title.  Lucky for me, they pulled this information from our HR database in the form of a CSV which saved me time editing and inputting the information into an excel spreadsheet and converting it to a CSV.

With this information in hand I could get to work.  I needed a script that imported the CSV, and updated the provided information.  This is what I came up with:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#set UserUpdate = to the path to the file

$UserUpdate = Import-Csv -Path "\\server\AlexG\ADUserUpdateScript\ADUpdate1.csv"

foreach($user in $UserUpdate){

        # Find user

        $ADUser = Get-ADUser -Filter "displayname -eq '$($user.name)'"

   if ($ADUser){

        Set-ADUser -Identity $ADUser -Title $user.jobtitle

        Set-ADUser -Identity $ADUser -Department $user.department

       Set-ADUser -Identity $ADUser -Office $user.office

 }

}

By all means this is not a sophisticated script and it definitely could use some refining but it worked well enough for my purposes. 

Starting from the top, this script creates the variable $UserUpdate and sets it equal to a commandlet. The commandlet Import-Csv does exactly what its says, it imports CSV files. The -Path "\\server\AlexG\ADUserUpdateScript\ADUpdate1.csv" tells the machine where the CSV file is located. 

So far we have created a variable and set it equal to a CSV file and had it imported.

Next we have a foreach statement.  It tells the script that for every name listed under the User column do something. this does two things, it tells the script to start with the first name and do the following commands.    

Before we list the commands, we want to create another variable.  I created a variable $ADUser and set it equal to the Get-ADUser commandlet.  Get-ADUser will get a list of all Active Directory users.  We don't want that.  We just want the user we are working on.  So I put some parameters in it.  I tell it to filter all of the users by their display name, an find the one that is the same ad the name listed in the User column in the CSV file.

So far we have imported a CSV file, we have isolated a name on the file and created a variable that equals that user in AD. Now we can tell the script what to do to that user but i wanted to make sure that we don't overwrite the wrong users so I put in an IF statement.

So IF the variable $ADUser has the same name as the user listed in the CSV, do the following commands.  The following commandlets (Set-ADUser) tells the script to overwrite the information that is present and replace it with the information provided in the CSV.  Again I am using parameters to isolate the information that I want changed.  These parameters are represented as -Title, -Department, -Office. The information that we want to put in those location is represented as $user.jobtitle, where the ".jobtitle" represents the name of the column where the information is located in the CSV file.  

So to surmise, this script imports information from a CSV file, and writes that data to specific locations under specific users.  

Definitely a complicated explanation for a simple script.

Comments

Popular posts from this blog

A script to check a log file. If conditions are met the script will then delete, and copy in a file into the folder.

Laptop Battery Replacement

Bulk AD User creation Powershell Script