get-content input with space not being read by get-aduser in foreach loop

Welcome Forums General PowerShell Q&A get-content input with space not being read by get-aduser in foreach loop

  • This topic has 5 replies, 3 voices, and was last updated 1 month ago by
    Moderator
    .
Viewing 5 reply threads
  • Author
    Posts
    • #194033
      Participant
      Topics: 22
      Replies: 58
      Points: 178
      Rank: Participant

      Greetings,
      Have the following script which is reading data from a file which contains lines with blanks, for example
      Michelle Test
      Jackie Robinson

      The following script errors when reading the file containing spaces in the data:

      $admins = Get-Content -Path D:\PowerShell_temp\SR603855_admin_deletes.txt
      foreach ($admin in $admins) {
      # Review of Current Status
      Get-ADUser -Identity $admin -Properties * | Select-Object Name,DisplayName,Description,SamAccountName,PasswordLastSet,PrimaryGroup,MemberOf,Manager,AccountExpirationDate,SamAccountName.enabled
      # Check ADUser Account Status
      (Get-ADUser -Identity $admin).Enabled
      # Delete the User
      Remove-ADUser -Identity $admin -Confirm:$false
      }
      

      Quoting the variable $admin or the data doesn't seem to help, looking for some assistance

      Thank you in advance

      Norm

    • #194039
      Participant
      Topics: 2
      Replies: 1693
      Points: 3,368
      Helping Hand
      Rank: Community Hero

      It would have been helpful when you posted the complete error message as well.

      The parameter -Identity of the cmdlet Get-ADUser expects one of the 4 possible values:
      A distinguished name
      A GUID (objectGUID)
      A security identifier (objectSid)
      A SAM account name (sAMAccountName)

      Do you provide something like this with your text file?

    • #194042
      Participant
      Topics: 22
      Replies: 58
      Points: 178
      Rank: Participant

      Hello Olaf,
      Simply the Name, for example

      DistinguishedName : CN=Michelle Test,
      Enabled           : False
      GivenName         : Michelle
      Name              : Michelle Test
      ObjectClass       : user
      ObjectGUID        : 851d72ea-1888-46e4-8e72-75337f9c94e7
      SamAccountName    : mtest
      SID               : S-1-5-21-2000478354-1229272821-682003330-54577
      Surname           : Test
      UserPrincipalName : mtest@
      
    • #194060
      Participant
      Topics: 22
      Replies: 58
      Points: 178
      Rank: Participant

      Hello Olaf,
      Got it to work, didn't dawn on me until I ran get-aduser -properties * that the Displayname and Name were the same, at least in our AD.

      $admins = Get-Content -Path D:\PowerShell_temp\SR603855_admin_deletes.txt
      foreach ($admin in $admins) {
      # Find and Remove the Active Directory User
      Get-ADUser -Filter{DisplayName -like $admin}  | Remove-ADUser -Confirm:$False
      }
      

      Thanks for the input

      Norm

    • #194063
      Participant
      Topics: 2
      Replies: 1693
      Points: 3,368
      Helping Hand
      Rank: Community Hero

      Displayname and name can be ambiguous. I recommend using the sAMAccountName or the Distinguished Name ... they are unique.

    • #194045
      Moderator
      Topics: 3
      Replies: 87
      Points: 464
      Helping Hand
      Rank: Contributor

      By default, Get-Content should be reading each line of your text file into an array as a separate element, regardless of the content of the line. You could check this by echoing the contents of the array, e.g at line 2:

      echo $admins

      If you don't get an array as expected, try specifying that the information should be stored in an array, like this:

      $admins = @(Get-Content -Path D:\PowerShell_temp\SR603855_admin_deletes.txt)

      or like this:

      [Array] $admins = Get-Content -Path D:\PowerShell_temp\SR603855_admin_deletes.txt

      Here is some more information on arrays and Get-Content.

      The source of the text file may also be important – you may need to specify the encoding of the file for Get-Content using the -Encoding parameter (valid options are listed on the Get-Content documentation page linked above).
      This is important because PowerShell cmdlets are wildly inconsistent when it comes to their default encoding. Get-Content defaults to ANSI but others default to UTF-8. In particular, the end-of-line character may be different. If your text file was auto-generated by an application, or created on a *NIX platform and then transferred to Windows (DOS), it may not have the line break characters that Get-Content expects, which could result in the entire file being read in as a single line rather than individual lines (producing a single string variable rather than an array).

Viewing 5 reply threads
  • You must be logged in to reply to this topic.