PowerShell for Windows Developers: Takeaways from Advanced Installer’s Tech Session | Part 2
At Advanced Installer, we’ve been hosting weekly Tech Talks for over a year, where our developers share key insights, lessons learned, and in-depth explorations of topics that help us grow as a team. These sessions have been invaluable for us, and we’re excited to share some of the best takeaways with the wider developer community.
In our previous session, we explored how PowerShell can be a game-changer for Windows development by streamlining your workflows and automating everyday tasks.
Whether you're managing systems, automating processes, or handling data, PowerShell simplifies repetitive work and boosts overall productivity.
Missed our first tech session takeaways ? You can catch up here.
If you enjoyed that first dive into PowerShell session, you’re in for more. In this part two, we’ll build on what you’ve learned and introduce new features, techniques, and best practices to help you level up your scripting game.
Now, let’s roll up our sleeves and get into more PowerShell techniques that will boost your productivity.
PowerShell’s Evolution
Before diving into new features and tasks, let’s take a quick look at how PowerShell evolved over the time:
- Windows PowerShell – This version works only on Windows and is locked at version 5.1.
- PowerShell 7+ – The modern, open-source, cross-platform version built on .NET Core.
If you’re starting a new project or looking to upgrade, PowerShell 7 is the way to go. It offers faster execution, enhanced modules, and better error handling.
For the best scripting experience, we use Visual Studio Code with the PowerShell extension because it offers a powerful and user-friendly scripting environment.
Working with Variables in PowerShell
Variables are the foundation of any scripting language. In PowerShell, variables always start with the $ symbol, making them easy to spot.
If you want to check which variables are currently active just run:
Get-Variable # List all declared variables
Or navigate to the variable partition:
cd Variable dir # Display the environment variables
PowerShell is flexible with variable types, so you don’t have to define them. Still, I recommend defining them to keep your code cleaner and prevent sneaky bugs:
[int]$i = 1 # Integer variable [bool]$j = $false # Boolean variable
You can even initialize multiple variables in one line:
[int]$i, $j = 1, 2 # i gets 1, j gets 2
Pro Tip: Use descriptive names for your variables and define their types when possible. This will make your code much easier to read and maintain, and your team will thank you.
Comparing Values in PowerShell
PowerShell has its own set of comparison operators. Let’s take a look at them:
Operator | Meaning |
---|---|
-lt | Less than (<) |
-gt | Greater than (>) |
-le | Less than or equal (<=) |
-ge | Greater than or equal (>=) |
-eq | Equals (==) |
-ne | Not equals (!=) |
Here’s an example on how to use the operators:
$i = 5 $j = 10 Write-Output ($i -lt $j) # Outputs: True
Working with Files in PowerShell
File operations are a core part of scripting. PowerShell makes it easy to create, read, and modify files.
- Writing to a File
"Test from PowerShell" > file.txt # Writes to file.txt
- Reading a File
Get-Content file.txt # Reads file content
- Opening a File
Notepad file.txt # Opens the file in Notepad
These simple examples are really helpful for data handling and system configuration, or managing logs.
Enforcing Strict Mode for Better Scripts
By default, PowerShell allows you to use variables even if you haven’t declared them.
While convenient, it can also lead to some hard-to-spot bugs.
$k = 4 Write-Output “K is $k” # Outputs: K is 4 Write-Output “X is $x” # No error, but $x is empty
The missing variable does not throw an error, it just returns nothing.
To catch these issues early, enable strict mode. This way, your scripts will be reliable and easier to debug.
Set-StrictMode -Version Latest
Strict mode enforces good coding practices and makes your scripts more predictable and easier to debug.
However, there are multiple levels of strict modes you can use (1.0, 2.0, 3.0), with varying levels of strictness.
Creating Functions in PowerShell
Organizing your scripts into functions is a best practice – it improves readability, reusability and maintainability.
Here are some examples of working with functions:
- Basic function definition
Function Print-Number($n) { Write-Output “The number is $n” } Print-Number 20 # Calls the function
- Using named parameters for readability
Function Print-Numbers { param( [int]$n, [int]$m ) Write-Output "N is $n" Write-Output "M is $m" } Print-Numbers -n 40 -m 50
- Returning values from functions
Function Add-Numbers($n, $m) { return $n + $m } Write-Output (Add-Numbers 3 4) # Returns 7
Do not invoke functions using C-style comma-separated arguments like Add-Numbers(3,4).
PowerShell interprets this syntax as calling the function with a single array parameter containing all provided arguments.
The function receives [3,4] as one parameter which can lead to unexpected behavior.
Debugging and Verbose Output
Debugging is crucial for writing reliable scripts.
PowerShell provides verbose output and debugging streams to help diagnose issues.
$VerbosePreference = 'Continue' $DebugPreference = 'Continue' $InformationPreference = 'Continue'
These settings provide detailed information during script execution, which is really helpful for troubleshooting.
Conclusion
PowerShell isn’t just another shell—it’s a powerful scripting language that makes it easier to automate, manage, and configure Windows environments.
By mastering its core concepts, from variables and functions to file handling and strict mode, you'll be able to write robust, efficient scripts that save time and reduce errors.
If you missed the first session, catch up here and start from the beginning. It's packed with fundamentals every IT pro should know.
Want to stay sharp and ahead of the curve? Subscribe to our Blog.