3 min read
Using enumerations in PowerShell (1/2)
Tobias & Aleksandar's tip #1:
The two very well-known PowerShell experts have teamed up to share their best and most...
PowerShell Tips
The two very well-known PowerShell experts have teamed up to share their best and most helpful PowerShell tips.
We will be publishing their scripts over the course of Scriptember in 13 blog posts. Don't miss their insights! Be sure to follow all Scriptember events, listed in our calendar here.
There are two important configuration variables in PowerShell that have great impact on formatting, yet aren’t always seen.
$ofs (output field separator) contains one space by default. It is the character(s) that PowerShell uses when automatically turning arrays into strings:
PS C:\> $lottery = 1..49 | Get-Random -Count 7
PS C:\> "Lottery Numbers: $lottery"
Lottery Numbers: 22 2 48 44 23 13 4
PS C:\> $ofs = ', '
PS C:\> "Lottery Numbers: $lottery"
Lottery Numbers: 22, 2, 48, 44, 23, 13, 4
PS C:\> $ofs = ','
PS C:\> "Lottery Numbers: $lottery"
Lottery Numbers: 22,2,48,44,23,13,4
$FormatEnumerationLimit defaults to 4. This is the number of array elements that PowerShell includes in any string conversion preview. Here is an example:
Get-Service |
Where-Object { $_.DependentServices.Count -gt 1 } |
Select-Object -Property Name, Dep*
You get a list with services that have at least 2 dependent services. When you look closely at the results, you see that a maximum of 4 dependent services are visible. If a service has more, the list is cut off, and "…" is shown:
NlaSvc {NcdAutoSetup, AppVClient, netprofm}
nsi {WlanSvc, icssvc, Wcmsvc, upnphost...}
p2pimsvc {PNRPAutoReg, p2psvc, PNRPsvc}
PNRPsvc {PNRPAutoReg, p2psvc}
ProfSvc {XblGameSave, TokenBroker, UserManager, shpamsvc...}
SamSs {MSDTC, KAPSService, LanmanServer, KtmRm}
When you set $FormatEnumerationLimit to any other number, you can control this limit. Setting it to -1 disables any limit and always shows all elements:
Now all entries are visible:
NlaSvc {NcdAutoSetup, AppVClient, netprofm}
nsi {WlanSvc, icssvc, Wcmsvc, upnphost, SSDPSRV, NcdAutoSetup, AppVClient, netprofm, NlaSvc, KND...
p2pimsvc {PNRPAutoReg, p2psvc, PNRPsvc}
PNRPsvc {PNRPAutoReg, p2psvc}
ProfSvc {XblGameSave, TokenBroker, UserManager, shpamsvc, NaturalAuthentication, Appinfo}
SamSs {MSDTC, KAPSService, LanmanServer, KtmRm}
Except, with "nsi" in this example, there is still a "…" and cut-off. This time, however, it was triggered by the console space constraints. To overcome these as well, you’d need to add Format-Table:
Get-Service |
Where-Object { $_.DependentServices.Count -gt 1 } |
Select-Object -Property Name, Dep* |
Format-Table -Wrap -AutoSize
Now the output finally lists all output without cutting off anything:
NlaSvc {NcdAutoSetup, AppVClient, netprofm}
nsi {WlanSvc, icssvc, Wcmsvc, upnphost, SSDPSRV, NcdAutoSetup, AppVClient, netprofm, NlaSvc,
KNDBWM, KAPSService, Netman, NcaSvc, SessionEnv, Netlogon, LanmanWorkstation, Killer Network
Service, jhi_service, IpxlatCfgSvc, iphlpsvc, XboxNetApiSvc, IKEEXT, hns, Dnscache,
WinHttpAutoProxySvc, Dhcp}
p2pimsvc {PNRPAutoReg, p2psvc, PNRPsvc}
PNRPsvc {PNRPAutoReg, p2psvc}
ProfSvc {XblGameSave, TokenBroker, UserManager, shpamsvc, NaturalAuthentication, Appinfo}
SamSs {MSDTC, KAPSService, LanmanServer, KtmRm}
During Scriptember, our calendar provides the best possible overview.
Sep 30, 2024 by Dr. Tobias Weltner and Aleksandar Nikolić
The two very well-known PowerShell experts have teamed up to share their best and most...
Sep 30, 2024 by Dr. Tobias Weltner and Aleksandar Nikolić
The two very well-known PowerShell experts have teamed up to share their best and most...
Sep 30, 2024 by Aleksandar Nikolić and Dr. Tobias Weltner
The two very well-known PowerShell experts have teamed up to share their best and most...
Tobias Weltner and Aleksandar Nikolić joinly wrote the blog post series 'Tobias&Aleksandar's PowerShell tips'. So we introduce both of them here:
--------------------
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.
--------------------
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.