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

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


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?

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@

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).

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


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