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.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,548 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,527 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,796 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.