4 min read
Understanding PowerShell Arrays
PowerShell arrays are a powerful and versatile way of managing collections of data, enabling you to efficiently...
Unlocking the Power of PowerShell: Tips for Success
Tired of cumbersome PowerShell scripts? Learn how direct assignment of variables can simplify your code and significantly increase performance.
When you need to store the results of a command, you naturally assign it to a variable:
$result = Get-Process
However, when you need to store results produced by a loop, many users resort to manual array handling:
# starting with an empty array
$result = @()
$list = 'Tobias',' Chrissy', 'Alex', 'Melinda', 'Jordan', 'Jeffrey'
foreach ($element in $list)
{
# manually putting results into array
$result += "processing $element"
}
$result.count
$result
Why not simply assign the result to a variable again and let PowerShell take care of all the array handling?
$list = 'Tobias',' Chrissy', 'Alex', 'Melinda', 'Jordan', 'Jeffrey'
$result = foreach ($element in $list)
{
# not assigning information turns it into a return value
"processing $element"
}
$result.count
$result
This is much easier, and it’s about 100x faster, too.
Note that you can assign anything to a variable—whether it’s a command, a control structure such as a loop, or even a condition. Most people write like this:
$date = Get-Date
if ($date.Hour -lt 12)
{
$text = 'morning'
}
else
{
$text = 'evening'
}
"It is $text."
Why not assign the result to a variable just once and take it directly from the control structure:
$date = Get-Date
$text =
if ($date.Hour -lt 12)
{
'morning'
}
else
{
'evening'
}
"It is $text."
Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.
The poster is available for download and in paper form.
Jan 7, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
PowerShell arrays are a powerful and versatile way of managing collections of data, enabling you to efficiently...
Dec 27, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
Tired of hidden errors in your PowerShell scripts? Discover how validation and transformation attributes can bring...
Dec 20, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
Want to make your PowerShell scripts faster and more flexible? Learn how to optimize the pipeline with script blocks...
Tobias Weltner and Aleksandar Nikolić joinly wrote the blog post series 'Tobias&Aleksandar's PowerShell tips'. So we introduce both of them here:
----------------------------
Aleksandar Nikolić is a Microsoft Azure MVP and co-founder of PowerShellMagazine.com, the ultimate online source for PowerShell enthusiasts. With over 18 years of experience in system administration, he is a respected trainer and speaker who travels the globe to share his knowledge and skills on Azure, Entra, and PowerShell. He has spoken at IT events such as Microsoft Ignite, ESPC, NIC, CloudBrew, NTK, and PowerShell Conference Europe.
----------------------------
Tobias is a long-time Microsoft MVP and has been involved with the development of PowerShell since its early days. He invented the PowerShell IDE "ISESteroids", has written numerous books on PowerShell for Microsoft Press and O'Reilly, founded the PowerShell Conference EU (psconf.eu), and is currently contributing to the advancement of PowerShell as member in the "Microsoft Cmdlet Working Group". Tobias shares his expertise as a consultant in projects and as a trainer in in-house trainings for numerous companies and agencies across Europe.