How to Create Folder on Remote Computer Using PowerShell

Problem

In this blog post, we will walk you through how to create folder on remote computer using PowerShell.

Create Folder on Remote Computer

To connect to remote computer, we can use Enter-PSSession that will create interactive session or Invoke-Command which is not interactive.

In this context, we have two computers named vm1 and vm2 respectively. The client will be vm1 and we will create folder on vm2 machine.

Since the computers are not in the same domain, we should use Windows Remote Management (WinRM) for remote communication or remoting.

Below are several prerequisites to use WinRM for remote communication:

  1. Enable Windows Remote Management (WinRM) service on both computers (vm1 and vm2)

Set-Service -Name WinRM -Status Running -StartupType Automatic

  1. Add servers we want to connect to TrustedHosts list on client computer, i.e., apply below script to vm1

Set-Item -Path WSMan:\localhost\Client\TrustedHosts -Value 'vm2'

  1. Enable firewall rule for WinRM to allow other IPs to connect on remote computer, so apply below script to vm2

Set-NetFirewallRule -Name 'WINRM-HTTP-In-TCP' -RemoteAddress Any
Enable-NetFirewallRule -Name 'WINRM-HTTP-In-TCP'

Or you can also do it manually:

1. Open Windows Defender Firewall with Advanced Security
2. Click Inbound Rules
3. Double-click Windows Remote Management (HTTP-In) for the Public profile
4. Click the Scope tab
5. Under Remote IP address, set it to `Any IP Address`
6. Enable Rule
Set-NetFirewallRule -Name 'WINRM-HTTP-In-TCP' -RemoteAddress Any Enable firewall rule for Windows Remote Management (HTTP-In)

Using Enter-PSSession cmdlet

To create session to remote computer, first you need to run Enter-PSSession and specify the computer name we want to connect as well as the credential (username and password).


Enter-PSSession vm2 -Credential (Get-Credential)

connect to remote computer using enter-pssession

Secondly, after connected we can execute the script to create folder on vm2 machine just like we do on local computer as follows:


New-Item -ItemType Directory -Path "C:\MyFolder"

Lastly, to close the session we use Exit-PSSession cmdlet.


Exit-PSSession

create folder on remote computer using New-Item

Using Invoke-Command cmdlet

With this cmdlet, you won’t have interactive session. The Get-ItemPropertyValue cmdlet will be executed in one go in a ScriptBlock.


Invoke-Command -ComputerName vm2 -ScriptBlock { 
    New-Item -ItemType Directory -Path "C:\MyFolder"
} -Credential (Get-Credential)

You still have to enter credential before the ScriptBlock is executed.

using invoke-command to create folder on remote computer

The result will look as follows:

create folder result using invoke-command

Conclusion

To create folder on remote computer using PowerShell, first we need to establish connection to remote computer (remoting). If the computers are not in the same domain, we should use Windows Remote Management (WinRM).

Then, we can use Enter-PSSession to create interactive session or Invoke-Command that will invoke the script in one go. These methods later will be combined with New-Item cmdlet to create folder on remote computer.

To create folder using PowerShell, you can use New-Item cmdlet and specify Directory as the value of -ItemType parameter.