powershelltolowercase-conversion

Convert output of a command to lowercase in powershell


I'm running a command in PowerShell to get the list of emails in a distribution group but I need to normalize the list to do some comparisons later and I need them all in lowercase. I've tried multiple instances of .tolower() but nothing seems to work. How can I achieve this?

Get-AzureADGroup -ObjectId [REDACTED] | Get-AzureADGroupMember -All $True |Select-Object UserPrincipalName | Sort-Object UserPrincipalName

This gives me the output:

UserPrincipalName
-----------------
First.Name@redacted.com
Jane.Doe@redacted.com
johnsmith@redacted.com
That.Person@redacted.com

I want them all in lowercase and, since we're already here, without the header "UserPrincipalName -----------------" as well. Thanks in advance.

Tried adding "tolower.()" on multiple different positions in the string. None worked. I'm getting an error that tolower is not recognized, but couldn't find how to install it

tolower. : The term 'tolower.' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ tolower.(Get-AzureADGroup -ObjectId [REDACTED] ...
+ ~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (tolower.:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Solution

  • You're using Select-Object in this case to create new objects having a property name UserPrincipalName. If you want to output strings instead, you can use ForEach-Object to expand on each .UserPrincipalName value:

    Get-AzureADGroup -ObjectId [REDACTED] |
        Get-AzureADGroupMember -All $True |
        ForEach-Object { $_.UserPrincipalName.ToLower() } |
        Sort-Object