3 min read
Decoding URLs: PowerShell's hidden gems
In this article, we explore how PowerShell handles URL encoding and decoding using two powerful APIs:...
Unlocking the Power of PowerShell: Tips for Success
Ready to level up your PowerShell enum game? Discover how [Flags()] transforms simple enums into powerful binary combinations, allowing you to manage multiple settings with elegant precision – perfect for feature flags, permission systems, and configuration management.
Simple enums define a list of allowed values, and the user can pick exactly one. When enums are defined as a flag array, then the user can pick more than one value:
[Flags()]
enum Features
{
BootLog
RealTimeProtection
Firewall
PhysicalAccess
Leash
}
[Features]$myFeatures = 'Firewall', 'Leash'
$myFeatures
By adding the [Flags()] attribute to the enum, each enum entry now gets its own unique bit value, thus the user can combine as many enum items as needed. The type [Enum] can shed some light into this. Here it lists the defined enum names:
Next, it shows the assigned true numeric values:
When you assign multiple values, bit values are added:
There is just one problem: the assigned numbers are consecutive and thus ambiguous – PhysicalAccess has ID 3, but RealTimeProtection+Firewall also adds to 3.
When you use [Flags()], you also need to manually assign unique values to the enum items:
[Flags()]
enum Features
{
BootLog = 1
RealTimeProtection = 2
Firewall = 4
PhysicalAccess = 8
Leash = 16
}
Now each item has a unique ID:
Now, with unique IDs, you can use enums like any other numbers to do binary arithmetic:
Use the "+=" operator to add an element, and "-=" to remove it:
Likewise, when testing whether a given value is present in such a list, use binary operators like -band and -bor:
Did you enjoy Scriptember? We did! Thanks for being part of it and #KeepOnScripting
Oct 1, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
In this article, we explore how PowerShell handles URL encoding and decoding using two powerful APIs:...
Mar 11, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
Most PowerShell cmdlets are simply wrappers around underlying .NET libraries. In this series, we’ll take a closer look...
Sep 30, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
Tired of cryptic number codes cluttering your PowerShell scripts? Discover how PowerShell enums can transform your code...
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.