Level up your PowerShell with five tips in five minutes!

Nathan Magyar
3 min readNov 23, 2020

--

Picture of binary in the shape of a love heart to play on my love of PowerShell and code.
I love PowerShell and so should you!

As engineers, we’re fortunate enough to have the most diverse tool in Microsoft administration right at our fingertips. With these 5 tips, you’ll start to understand just how easily you can leverage PowerShell to your advantage in your daily operations.

  1. Use pipeline inputs
    Something I did too often when I first started learning PowerShell was casting everything and anything as a variable only to use it once in the very next line. Why do that, when you can simply pass the output down the pipeline into your next command?

Instead of a convoluted and messy block of code as below:

$files = Get-ChildItem -Path C:\Temp\* -Include *.txt
forEach ($file in $files) {
Remove-Item $file
}

Try using the pipeline to your advantage to complete the same action:

Get-ChildItem -Path C:\Temp\* -Include *.txt | Remove-Item

2. Get familiar with forEach loops
forEach loops are a staple in my scripts. They allow you to seamlessly execute actions on many inputs in rapid succession. This is a huge time saver. Let’s say for example HR have requested you to add 50 users to the new Company Events group and change their department to “Events Team”— instead of making changes to all 50 users individually, you could simply run something like the below:

$users = Get-ADUser -Filter * -Properties Department | Where {$_.Department -eq "*Events*"}
forEach ($user in $users) {
Add-ADGroupMember -Identity "Company Events" -Members $user
Set-ADUser -Identity $user -Department "Events Team"
}

3. Get a great text editing software
Writing any form of code is near impossible for a beginner without syntax highlighting. I started out writing in PowerShell ISE. Whilst this is a decent debugging tool, it’s archaic and dated interface could use a face-lift. Nowadays, I use Visual Studio Code. It’s free and it looks great.

A screenshot by the author showing PowerShell syntax highlighting in Visual Studio Code.
Visual Studio Code — isn’t it pretty?

4. Harness the power of parentheses!
Parentheses can be used to grab specific values from your output. Next time you run a “Get-” cmdlet, try something like the below:

(Get-MsolUser).Identity

In this example, instead of getting the user, and all the unnecessary data associated with their object – you’ll simply get the users Identity. To achieve this, all I’ve done is:
I) Wrapped the command in parentheses
II) Added a period, followed by the property you wish to query

5. Script EVERYTHING
Practice makes perfect. Scripting as much as possible will not only teach you more about PowerShell, it’ll also help you gain a comprehensive understanding of the product you’re administering. You might find that it takes you longer to complete the task initially, but the next time you need to perform the same task, it’ll take you just seconds!

I’ve included some examples in a file that can be found below, be sure to check it out.

--

--

Nathan Magyar
Nathan Magyar

Written by Nathan Magyar

A Systems Engineer with a passion for all things PowerShell based in Melbourne, Australia. https://www.linkedin.com/in/nathanmagyar/

No responses yet