Net User in PowerShell

28,814

Solution 1

The reason the error output is not being appended to the log file is because you are only redirecting the STDOUT (standard output stream). To also redirect the STDERR (standard error stream) change

net user $user /domain >> $CompLog

to

net user $user /domain 2>&1 $CompLog

This explains it a bit more: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_Pipes_and_Redirection#Windows_PowerShell_Redirection_Operators

Solution 2

Sorry, I don't have enough reputation to add a comment.

When you run programs in Powershell (such as net user... or ping) it should be running exactly the same as it would in the normal command prompt (cmd).

If I understand correctly you're getting different results when you (effectively) run the same thing in Powershell or Command Prompt. Is that right?

Are you using the ISE to build your script? If so you can set Breakpoints that will pause the script and allow you to see what the variables are. It could be that the $user variable isn't holding what you think it should be, or that the value doesn't match the name for a domain user account.

EDIT:

What happens when you run the net user ... command interactively (e.g. not as a script, but manually) in Powershell? Do you get an error? If so, what does the error say?

Additional related stuff:

You shouldn't need the Start-Sleep as the commands are run in order, and the next line shouldn't execute until the previous on has completed.

Also, which version of Powershell are you using?

(You can check by running $host.version.Major)

The Get-ADUser cmdlet requires version 3 (I believe) and also needs to import the Active Directory module.

Share:
28,814
JBnAZ
Author by

JBnAZ

Updated on October 17, 2020

Comments

  • JBnAZ
    JBnAZ over 3 years

    I am in the middle of moving to the cloud, migrating from SBS 2003 Active Directory, to 2008 R2.

    I configured a new user, and noticed that the user was unable to reset their password.

    My Server Admin showed me how to use net user.

    I noticed that I can obtain information from some accounts and not others. With over 100 accounts to process, I thought I'd try PowerShell.

    In this post (Use powershell to look up 'net user' on other domains?) Lorenzo recommends using Get-ADUser (and this applies to polling from another domain). When I run Get-ADUser from my PowerShell prompt, I receive a message stating that the commandlet is not recognized.

    I am reading the user IDs from a text file, and sending the output to a log file so that I can send to the server admin for further analysis.

    Here is my code so far (please note that I am completely new to PowerShell):

    # Get our list of user names from the local staff.txt file
    $users = get-content 'C:\Scripts\staff.txt'
    
    # Create log file of output:
    $LogTime = Get-Date -Format 'MM-dd-yyyy_hh-mm-ss'
    $CompPath = "C:\Scripts\"
    $CompLog = $CompPath + "NetUserInfo" + $LogTime + ".txt"
    New-Item -path $CompLog -type File
    
    foreach ($user in $users) {
    
    #Testing user:
    "Testing user: $user" | out-file $CompLog -Append
    
    # Obtain user information using net user:
    net user $user /domain >> $CompLog
    
    # Pause to let system gather information:
    Start-Sleep -Second 15
    
    }
    

    As the script runs currently, my log file will have two or three user names followed by the response "The request will be processed at a domain controller for domain (domain)"

    If net user, from CMD, would return "System error 5 has occurred, Access is denied." This is not logged in the output file. IF net user, from CMD, would return user information, this is logged to the output file. I am currently receiving output for only a couple users, but when I run the command from CMD, I am able to retrieve information for at least ten.

    My first thought was that I needed to wait for the net user command to complete (hence the Start-Sleep command) but that has not had any effect on the output.

    Any assistance would be greatly appreciated.

    • alroc
      alroc over 10 years
      Use Get-ADUser to get user information in PowerShell instead of calling net. You'll be able to control it better and make better use of the data.