≡ Menu

Connect to remote powershell with alternate credentials

In my previous post, I talked about how to enable remoting feature and testing the connection. While playing with options of remote execution of commands, I felt why should I always use a powershell window which is opened with credentials that has admin rights on remote machine. I know the admin password of remote machine and started to look for the procedure where I can make use of it and execute remote commands. Finally ended with below procedure.

I know the local administrator password of compB and I have a powershell session opened on CompA with normal rights. I used the below steps to establish a connection and execute commands.

establishing connection…

[PS] C:> $remote = New-Pssession -ComputerName CompB -Credential administrator

Executing above, prompts for password of administrator account on CompB, so please do that to proceed.

Once the connection is established, you can issue commands using the established session like shown below

[PS] C:>invoke-command $remote {Get-Process}

This displays the process of remote machine.

Once you are done with you operation, you may choose to kill that session issuing below command

[PS]C:>remove-PsSession $remote

That’s it. Enjoy remote execution…

Happy Learning..,
Sitaram Pamarthi

Comments on this entry are closed.

  • Carl Wayne April 21, 2011, 3:05 am

    none of this works! I have no idea what I am doing wrong. Please help!

  • Sitaram Pamarthi April 22, 2011, 9:17 pm

    Ensure that you have remoting enabled on remote computer that you are trying to connect. http://techibee.com/powershell/how-to-enable-remoting-in-powershell-v2/248

  • Kristian Henderson September 15, 2015, 9:03 pm

    It doesn’t work because its not “-Credentials” its “-Credential”

    • TechiBee September 20, 2015, 7:27 am

      Thanks Kristian for highlighting the typo. I corrected it.

  • laxman July 22, 2016, 5:32 pm

    Hi,

    I have below two queries.

    Query1:
    My requirement is to establish a connection with a remote machine and copy the folders into one of its drive. Despite running the powershell in administrator mode, I am still prompted to give the password. is my below command correct , please verify and let me know.

    Invoke-Command -ComputerName \\\ -ScriptBlock { Get-ChildItem C:\ } -credential get-credential

    Query2:
    While performing the copy-item (nested), one of the files in the subfolder is being copied in the root folder too. Ideally, the structure along with the files and subfolders should be copied in a consistent manner. I am excluding few folders from being copied.
    Scenario:
    Source has the below structure.
    Rootfolder>Subfolder1>Subfolder1_1>demo.txt
    Rootfolder>Subfolder2
    Rootfolder>Subfolder3
    Rootfolder>sample.txt

    after running the below command:-

    $ExcludeFolders = @(“subfolder2”)
    Get-ChildItem -Path $FROM -Exclude $ExcludeFolders |
    Copy-Item -Destination $TO -force -recurse

    The destination folder is coming up this way:
    Rootfolder>Subfolder1>Subfolder1_1>demo.txt
    Rootfolder>Subfolder3
    Rootfolder>sample.txt
    Rootfolder>demo.txt

    please answer my above two queries

    Thanks,
    laxman