Output with Invoke-Command

Welcome Forums General PowerShell Q&A Output with Invoke-Command

This topic contains 4 replies, has 4 voices, and was last updated by

9 months, 1 week ago.

  • Author
  • #122885

    Topics: 1
    Replies: 1
    Points: 14
    Rank: Member


    Powershell noob here. Have a question regarding the output when you invoke a scriptblock with Invoke-Command.

    I am trying to do bulk password resets with names stored in an array and passing that array through a foreach loop to Set-ADAccountPassword. If I run this command, my write-host outputs that only the first name in the array had the password changed. But if I run this same code directly from the DC, it correctly says that all the users passwords have been changed.

    Is this simply an output issue with Write-Host when using Invoke-Command? Or is really only 1 of the users named being passed across to the other machine?


    $UserN = @("one","two","three","four")
    $Cred = ....
    Invoke-Command -ComputerName XXX.XX.XX.XX -Credential $Cred -ArgumentList $UserN `
    foreach ($user in $UserN){
    Set-ADAccountPassword -Identity $user -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "XXXXXX" -Force)
    Write-Host "`nPassword changed for $user!" -ForegroundColor Yellow 
  • #122889

    Topics: 0
    Replies: 12
    Points: 112
    Team Member
    Rank: Participant

    It looks like you are running / invoke that on a remote machine?

    Do you have PowerShell v3 or newer?

    $UserN = "one", "two", "three", "four"
    $Cred  = 'P@ssw0rd'
    Invoke-Command -ComputerName XXX.XX.XX.XX -Credential $Cred -ScriptBlock {
        foreach ($user in $using:UserN) {
            Set-ADAccountPassword -Identity $user -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "XXXXXX" -Force) -verbose
    • #122900

      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      @alexw : Yes, I should have clarified. I am using Invoke-Command to invoke on a remote machine which is also on another domain. Yes latest PS versions are installed.

      @Wes : Don't I still need to use Invoke-Command in order to execute the command on the remote machine?

      Should have also clarified better: I am not using Invoke-Command when running the code from the remote machine. I am just using the basic foreach loop through the array, which works fine. It's only when I use Invoke from my machine to the target machine that the Output says only the first user in the array had the password changed.

  • #122894

    Topics: 2
    Replies: 50
    Points: 312
    Team MemberHelping Hand
    Rank: Contributor

    Set-ADAccountPassword can accept pipeline input. So in addition to not needing to use Invoke-Command, you can get rid of the foreach loop as well.
    Something like

    $userN | Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "XXXXXX" -Force)

    should work.

  • #122897

    Senior Moderator
    Topics: 8
    Replies: 1004
    Points: 3,253
    Helping Hand
    Rank: Community Hero

    You could use implicit remoting, below article explains the beauty of it.


The topic ‘Output with Invoke-Command’ is closed to new replies.