2 min read
Boost PowerShell efficiency: How to use enums for cleaner code
Tired of cryptic number codes cluttering your PowerShell scripts? Discover how PowerShell enums can transform your code...
Unlocking the Power of PowerShell: Tips for Success
Tired of wading through empty properties in PowerShell objects? Learn how to filter out unnecessary blanks automatically! With PowerShell's PSObject extension, you can create a custom function to display only populated properties – keeping your results clean and relevant. Perfect for streamlining data views without manual tweaks!
Occasionally, you receive objects with a lot of empty properties that you are not really interested in. You could use Select‑Object to manually select properties, but if the empty properties change, an automated solution would be helpful.
Fortunately, PowerShell adds a secret PSObject extension to each object that reveals the entire object structure, including properties and their values. This way, a simple filter function would look like this:
filter Filter-EmptyProperty
{
$hashtable = [Ordered]@{}
foreach ($property in $_.PSObject.Properties)
{
if ($property.Value)
{
$hashtable[$property.Name]=$property.Value
}
}
[PSCustomObject]$hashtable
}
In essence, for each object, all of its properties are checked for a value, and only if a value is found, then the property is added to an ordered hash table. Once all desired properties have been collected, the ordered hash table is turned to an object and emitted.
Try these commands (or any other source command that returns empty properties):
# includes empty properties
Get-ComputerInfo -Property *
# shows only populated properties
Get-ComputerInfo -Property * | Filter-EmptyProperty
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.
Sep 30, 2024 by Dr. Tobias Weltner and Aleksandar Nikolić
Tired of cryptic number codes cluttering your PowerShell scripts? Discover how PowerShell enums can transform your code...
Sep 30, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
Say goodbye to hardcoding paths and settings! Learn how to simplify your PowerShell scripts by storing config data...
Sep 30, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
Ready to level up your PowerShell enum game? Discover how [Flags()] transforms simple enums into powerful binary...
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.