0

So I have this PowerShell one-liner that gives me back my groups:

$groups = Get-ADPrincipalGroupMembership userName | ForEach-Object {Write-Host $_.Name -BackgroundColor Red} |Out-String; $groupsWithSpaces = foreach ($groups in $groupsWithSpaces) {Select-String -AllMatches '[\\s+,]' | Write-Host}; Write-Host $groupsWithSpaces

How am I using the regex wrong? I am trying to return only groups that have a whitespace on the end. I am always returning every group, and I only want the ones that have white space on the end. I can get true/false, but...help please.

Thanks!

2 Answers 2

1

In your regex you were escaping one of the slashes. Making it so you were looking for groups with a literal slash followed by at least on letter s.

If you are just trying to find the group with spaces in them this would work

Get-ADPrincipalGroupMembership username | 
    Where-Object{$_.Name -match "\s"} | 
    select -ExpandProperty Name | 
    Write-Host -BackgroundColor red

I formatted it for easy reading

If you wanted to see the whole list with the empty ones standing out you could also use and if statement

Get-ADPrincipalGroupMembership username | select -ExpandProperty Name | ForEach-Object{
    If( $_ -match  "\s"){
        Write-Host $_ -BackgroundColor red
    } Else {
        Write-Host $_
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think this is the false variable --> $groupsWithSpaces = foreach ($groups in -->HERE<--$groupsWithSpaces-->HERE<--) {Select-String -AllMatches '[\\s+,]' | Write-Host}; Write-Host $groupsWithSpaces.
Work this one?

$groups = Get-ADPrincipalGroupMembership userName | ForEach-Object {Write-Host $_.Name -BackgroundColor Red} |Out-String; $groupsWithSpaces = foreach ($group in $groups) {If ($group[-1] -eq " ") {Write-Host $group}}; Write-Host $groupsWithSpaces

1 Comment

I can't paste the output, but it's returning the same as my variant. Although thanks for grabbing the false variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.