Unable to use trim in powershell select from array.

AintGotNoTime 161 Reputation points
2021-07-14T17:48:41.187+00:00

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 for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2021-07-14T18:49:37.883+00:00

    How about this?

    Select-Object @{Name="userName";Expression={$_.SamAccountName.Trim()}}, etc.
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.