foreach and out-file- no data

%3CLINGO-SUB%20id%3D%22lingo-sub-1842591%22%20slang%3D%22en-US%22%3Eforeach%20and%20out-file-%20no%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1842591%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20need%20to%20return%26nbsp%3B%20a%20list%20of%20active%20users%3A%26nbsp%3B%20Display%20name%20etc.%20but%20no%20data%20is%20exported%20to%20my%20out-file.%20Any%20clues%20would%20be%20highly%20appreciated%20.%20Thanks%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24users%20%3D%20Get-Content%20%22C%3A%5Cdump%5Cusernames2.txt%22%0Aforeach%20(%24user%20in%20%24users)%0A%7B%0Aif%20(Get-ADUser%20-ldapfilter%20%22(samaccountname%3D%24user)%22%20-Properties%20displayname%2Cmail%2CdistinguishedName%20%7C%20Select-Object%20-Property%20displayname%2Cmail%2CdistinguishedName)%0A%7B%0Aout-file%20%22c%3A%5Cdump%5Cusersexist.txt%22%0A%7D%0Aelse%0A%7B%0AAdd-Content%20%22C%3A%5Cdump%5Cnamenotfound.txt%22%20%22%24user%22%0A%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1842591%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eactivedirectory%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%20Commands%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1842611%22%20slang%3D%22en-US%22%3ERe%3A%20foreach%20and%20out-file-%20no%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1842611%22%20slang%3D%22en-US%22%3EI%20see%20you%20are%20using%20the%20-ldapfilter%20filter%20and%20then%20labeling%20a%20SamAcountName.%3CBR%20%2F%3E%3CBR%20%2F%3EAssuming%20you%20are%20piping%20in%20the%20SamAcountName.%3CBR%20%2F%3ETry%20Get%20-ADUser%20-identity%20%24_.%24User%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1842629%22%20slang%3D%22en-US%22%3ERe%3A%20foreach%20and%20out-file-%20no%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1842629%22%20slang%3D%22en-US%22%3E%3CP%3Eyes%2C%20the%20file%20contains%20SamAccountNames%3CBR%20%2F%3E%3CBR%20%2F%3EI%20tired%20the%20-identity%20para%20but%20received%20the%20error%20below%3CBR%20%2F%3E%3CBR%20%2F%3EGet-ADUser%20%3A%20Cannot%20validate%20argument%20on%20parameter%20'Identity'.%20The%20argument%20is%20null.%20Provide%20a%20valid%20value%20for%20the%20argument%2C%20and%3CBR%20%2F%3Ethen%20try%20running%20the%20command%20again.%3CBR%20%2F%3EAt%20line%3A4%20char%3A26%3CBR%20%2F%3E%2B%20if%20(Get-ADUser%20-identity%20(%24_.%24User)%20-Properties%20displayname%2Cmail%2Cdist%20...%3CBR%20%2F%3E%2B%20~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20InvalidData%3A%20(%3A)%20%5BGet-ADUser%5D%2C%20ParameterBindingValidationException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20ParameterArgumentValidationError%2CMicrosoft.ActiveDirectory.Management.Commands.GetADUser%3CBR%20%2F%3E%3CBR%20%2F%3EGet-ADUser%20%3A%20Cannot%20validate%20argument%20on%20parameter%20'Identity'.%20The%20argument%20is%20null.%20Provide%20a%20valid%20value%20for%20the%20argument%2C%20and%3CBR%20%2F%3Ethen%20try%20running%20the%20command%20again.%3CBR%20%2F%3EAt%20line%3A4%20char%3A26%3CBR%20%2F%3E%2B%20if%20(Get-ADUser%20-identity%20(%24_.%24User)%20-Properties%20displayname%2Cmail%2Cdist%20...%3CBR%20%2F%3E%2B%20~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20InvalidData%3A%20(%3A)%20%5BGet-ADUser%5D%2C%20ParameterBindingValidationException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20ParameterArgumentValidationError%2CMicrosoft.ActiveDirectory.Management.Commands.GetADUser%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1843168%22%20slang%3D%22en-US%22%3ERe%3A%20foreach%20and%20out-file-%20no%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1843168%22%20slang%3D%22en-US%22%3EI%20would%20always%20display%20my%20output%20first%20to%20be%20sure%20I%20was%20getting%20something%20returned%20to%20the%20console.%3CBR%20%2F%3EThen%20write%20the%20output%20to%20a%20file.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1843375%22%20slang%3D%22en-US%22%3ERe%3A%20foreach%20and%20out-file-%20no%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1843375%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F223343%22%20target%3D%22_blank%22%3E%40DarrenRD%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%3C%2FP%3E%3CP%3EWhat%20are%20you%20expecting%20from%20the%20out-file%2C%20where%20is%20the%20inputObject%2C%20at%20least%20you%20will%20need%20to%20pipeline%20it%20with%20another%20command.%3C%2FP%3E%3CP%3Eif%20you%20are%20talking%20about%20dumping%20the%20console%20output%20to%20a%20file%2C%20then%20this%20is%20a%20console%20redirection%2C%20%3CA%20href%3D%22https%3A%2F%2Fwww.powershellcenter.com%2F2020%2F08%2F14%2Fpowershell-dump-the-console-output-to-a-file-output-redirection%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Eread%20about%20it%20here.%3C%2FA%3E%3C%2FP%3E%3CP%3Eanyway%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24users%20%3D%20Get-Content%20%22C%3A%5Cusernames2.txt%22%0Aforeach%20(%24user%20in%20%24users)%0A%7B%0Atry%7B%0A%20%20%20%20%24Value%3DGet-ADUser%20-ldapfilter%20%22(samaccountname%3D%24user)%22%20-Properties%20displayname%2Cmail%2CdistinguishedName%20-ErrorAction%20stop%7C%20Select-Object%20-Property%20displayname%2Cmail%2CdistinguishedName%20%0A%20%20%20%20if%20(%24Value.count%20-eq%200)%7B%0A%20%20%20%20%20%20%20%20Add-Content%20%22C%3A%5Cnamenotfound.txt%22%20%22%24(%24user)%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%7B%0A%0A%20%20%20%20add-Content%20-Path%20'C%3A%5CUserFound.txt'%20-Value%20%24Value%20%0A%20%20%20%20%7D%0A%7D%0Acatch%7B%0AWrite-Host%20%24_.exception.message%0A%0A%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheck%20the%20code%20above%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi,

I need to return  a list of active users:  Display name etc. but no data is exported to my out-file. Any clues would be highly appreciated . Thanks

 

 

$users = Get-Content "C:\dump\usernames2.txt"
foreach ($user in $users)
{
if (Get-ADUser -ldapfilter "(samaccountname=$user)" -Properties displayname,mail,distinguishedName | Select-Object -Property displayname,mail,distinguishedName)
{
out-file "c:\dump\usersexist.txt"
}
else
{
Add-Content "C:\dump\namenotfound.txt" "$user"
}
}

 

I

 

4 Replies
I see you are using the -ldapfilter filter and then labeling a SamAcountName.

Assuming you are piping in the SamAcountName.
Try Get -ADUser -identity $_.$User

yes, the file contains SamAccountNames

I tried the -identity para but received the error below

Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and
then try running the command again.
At line:4 char:26
+ if (Get-ADUser -identity ($_.$User) -Properties displayname,mail,dist ...
+ ~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and
then try running the command again.
At line:4 char:26
+ if (Get-ADUser -identity ($_.$User) -Properties displayname,mail,dist ...
+ ~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

I would always display my output first to be sure I was getting something returned to the console.
Then write the output to a file.

@DarrenRD 

Hi

What are you expecting from the out-file, where is the inputObject, at least you will need to pipeline it with another command.

if you are talking about dumping the console output to a file, then this is a console redirection, read about it here.

anyway

 

 

 

$users = Get-Content "C:\usernames2.txt"
foreach ($user in $users)
{
try{
    $Value=Get-ADUser -ldapfilter "(samaccountname=$user)" -Properties displayname,mail,distinguishedName -ErrorAction stop| Select-Object -Property displayname,mail,distinguishedName 
    if ($Value.count -eq 0){
        Add-Content "C:\namenotfound.txt" "$($user)"
        }
        else{

    add-Content -Path 'C:\UserFound.txt' -Value $Value 
    }
}
catch{
Write-Host $_.exception.message

}
}

 

 

 

Check the code above