Remove characters from output in each row

177 Views Asked by At

I am a newbie to PowerShell and I am struggling to modify an output according to the way I want.

The command I run in MS Exchange Shell is this:

Get-Mailbox user1 | Get-ADPermission | ? {($_.ExtendedRights -like "*send-as*") -and -not ($_.User -like "NT AUTHORITY\SELF")} | Select User

The result I get from the above command is:

User              
----              
AGL\user4
AGL\user5
AGL\groupX

However I would like to obtain the output as:

User              
----              
user4
user5
groupX

Is it possible to do this?

Thanks heaps in advance.

2

There are 2 best solutions below

0
On BEST ANSWER

For this, use a Calculated Property to output the objects with their names only:

Get-Mailbox -Identity user1 | 
    Get-ADPermission | 
    Where-Object {($_.ExtendedRights -like "*send-as*") -and ($_.User -ne "NT AUTHORITY\SELF")} | 
    Select-Object @{Name = 'User'; Expression = {$_.User.Split("\",2)[1]}}
0
On

After a little testing, You can try

(Get-Mailbox user1 | Get-ADPermission | ? {($_.ExtendedRights -like "*send-as*") -and -not ($_.User -like "NT AUTHORITY\SELF")} | Select User).TrimStart("AGL\")

If that doesn't work you can try

$Rights = Get-Mailbox user1 | Get-ADPermission | ? {($_.ExtendedRights -like "*send-as*") -and -not ($_.User -like "NT AUTHORITY\SELF")} | Select User

$Rights = $Rights.TrimStart("AGL\")

The trim will trim the start up to the point of the back slash, as long as the string stays the same.