question

AintGotNoTime-6052 avatar image
0 Votes"
AintGotNoTime-6052 asked AintGotNoTime-6052 commented

Unable to use trim in powershell select from array.

I have some samAccountNames that when selected have a space at the end in AD, I have tried every which way to use Trim to get ride of it in the statement but i always get an error. What is the correct way to use it in this statement.

This is the main statment

 #Select the objects from the Get-ADuser and put in an array for use later
 $dvaacctArray = $dvamailboxAccounts | select @{Name="userName";Expression={$_.SamAccountName}}, @{Name="address";Expression={"my information"}}, 
       @{Name="safeName";Expression={'my info'}},@{Name="platformId";Expression={'My Info'}}, 
       @{Name="secret";Expression={'12345678'}}

Tried these
select Trim(@{Name="userName";Expression={$_.SamAccountName}}),
Error = A positional parameter cannot be found that accepts argument 'System.Object[]

select @{Name="userName";Expression=Trim({$_.SamAccountName}}),
Error = Unexpected token in expression or statement

select @{Name="userName";Expression={Trim($_.SamAccountName)}},
Error = Cannot convert 'Microsoft.ActiveDirectory.Management.ADPropertyValueCollection' to the type 'System.String' required by parameter 'Server'. Specified method is not supported


At a loss as how to make sure no spaces happen at end as if they are in the name it fails later on in my script when trying to load it into a specialty database.

windows-server-powershellwindows-active-directory
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered AintGotNoTime-6052 commented

How about this?

 Select-Object @{Name="userName";Expression={$_.SamAccountName.Trim()}}, etc.
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hmm, i tried this but did not try it as it was not coming up as a command highlight, so i did not really try. But it works, thanks.

0 Votes 0 ·