Home > Blockchain >  In Powershell how can I remove the first x number of characters from Get-ADUser results?
In Powershell how can I remove the first x number of characters from Get-ADUser results?

Time:11-26

I have a list of results from Get-ADUser giving me all users in an OU. The format of the output username is '-prefix-username'. I need to remove the 7 character '-prefix-' and then conduct another Get-ADUser lookup against the remaining 'username' portions. The issue I'm finding is that if I run just the second Get-ADUser lookup where I set $User as just one specific '-prefix-username' it works fine but when I try to process a list I either get an error where there seems to be space after the trimmed username (txt format list - Get-ADUser : Cannot find an object with identity: 'user ' under:) or the username includes a " that I can't remove from the end of the username (csv format list - Get-ADUser : Cannot find an object with identity: 'user"').

So far I have:

get-ADUser -Filter * -SearchBase 'OU=SomeOU' -SearchScope 2 | 
Select SAMAccountName | 
Out-File C:\Temp\UserList.txt

$UserList = (Get-Content C:\Temp\UserList.txt)

$StandardUsers = ForEach($User in $UserList) {
   
    Write-Host "Now checking $User"
    Get-ADUser $User.Substring(7) -Properties * | 
    Select-object DisplayName, UserPrincipalName, Mail, Manager,EmployeeID    
    }
    $StandardUsers | Out-File -FilePath C:\Temp\StandardUserList.txt

CodePudding user response:

First thing to mention is that if you create the list using Select -ExpandProperty SAMAccountName, you would only get SamAccountnames in the file.

Having said that, why bother with an 'in-between' file at all and simply do:

# By default, Get-ADUser returns these properties:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
# Only ask for properties that are not already in this list.
Get-ADUser -Filter * -SearchBase 'OU=SomeOU' -SearchScope 2 -Properties DisplayName, EmailAddress, Manager, EmployeeId |
    Select-Object DisplayName, UserPrincipalName, EmailAddress, Manager,EmployeeID |  
    Set-Content -Path 'C:\Temp\StandardUserList.txt'

CodePudding user response:

You are likely having issues with saving it to a file (where it gets formatted) and then reading it back in. The formatting could be adding " and reading a newline (which you think is a space) character. If you really need to save it then do the following (else just hook up the pipelines):

$userList = Get-ADUser -Filter * -SearchBase 'OU=SomeOU' -SearchScope 2 | 
  Select-Object SAMAccountName
$userList | 
  Out-File C:\Temp\UserList.txt

$standardUsers = $userList |
  Select-Object -ExpandProperty SAMAccountName -PipelineVariable user |
  ForEach-Object {
    Write-Host "Now checking $user"
    $userWithoutPrefix = ($user -Replace '^-prefix-','') -Replace '(\w|\n)$','' # to use a more advanced version of the suggestion by @Avshalom
    Get-ADUser $userWithoutPrefix -Properties * | Write-Output
  } | 
  Select-Object DisplayName, UserPrincipalName, Mail, Manager, EmployeeID

$standardUsers | Out-File -FilePath C:\Temp\StandardUserList.txt
  • Related