GitHub link to my scrips here: https://github.com/PhysioFred/PowerShell-Scripts
Recently, I’ve been exploring PowerShell to understand the fundamentals of scripting, focusing on variables, operators, arrays, and loops. These concepts are essential for automating administrative tasks in a Windows environment, particularly for managing Active Directory (AD) users.
Building Blocks: Variables, Operators, Arrays, and Loops
- Variables: Used to store data, variables allow for dynamic data handling in scripts.
- Operators: PowerShell operators, such as
-eq
(equals) and-ne
(not equals), are used to compare values and make decisions within scripts. - Arrays: Arrays are collections of values stored in a single variable. For example,
$list_of_users = @("aburtt", "acarron", "afegan")
holds multiple usernames. - Loops: Loops, like the
ForEach
loop, allow repetitive tasks to be automated by iterating over a collection of items.
Creating a Script to Manage Active Directory Users
To practice these concepts, I wrote a script to manage AD users by manipulating an array of usernames. This script demonstrates adding and removing users from the array and running a loop to remove them from AD.
Confirming the Results:
- After running the script, I confirmed that
"aburtt"
was successfully removed from both the array and not removed from the AD as you can see in the screenshot. - When attempting to find
"ahayne"
, theGet-ADUser "ahayne"
command returned an error, confirming that"ahayne"
did not exist in the AD after the script.
