SoFunction
Updated on 2025-04-08

Operation steps for folder sharing and disk mapping in Powershell

Folder Sharing Overview

The application of shared folders is very wide. The client manages the server's file, passes files directly on the LAN file, etc. Under Linux, the smaba protocol can be simply installed and can be used after simple configuration. Under Windows, this function can be enabled through graphical operations, of course, with a little geek style, we manage it through powershell (a small amount of cmd).

Operation steps

View the share list

Within powershell, we can get the shared information by executing the following cmdlet:

λ Get-WmiObject -Class Win32_Share
Name                                      Path                                      Description
----                                      ----                                      -----------
ADMIN$                                    C:\Windows                                Remote Management
C$                                        C:\                                       Default sharing
D$                                        D:\                                       Default sharing
E$                                        E:\                                       Default sharing
IPC$                                                                                remote IPC
Users                                     C:\Users

Similarly, under cmd, it is also possible

λ net share
Shared name       resource                            annotation
-------------------------------------------------------------------------------
C$           C:\                             Default sharing
D$           D:\                             Default sharing
E$           E:\                             Default sharing
IPC$                                         remote IPC
ADMIN$       C:\Windows                      remote管理
Users        C:\Users
The command completed successfully。

Create a shared folder

Crazy Powershell

# Shared Name$ShareName = 'TestShare'
# Shared path$Path = 'D:\SHARE'
If (!(Get-WmiObject -Class Win32_Share -Filter "name='$ShareName'")) 
{ 
	$Shares = [WMICLASS]"WIN32_Share" 
	$($Path,$ShareName,0).ReturnValue
}
else
{
	Write-Warning "$ShareName has been sharing!!"
}

If you have administrator rights on the remote machine, use WMI to create a new shared folder on the remote machine. Here is the code to create a shared folder on the remote host:

# Shared Name$ShareName = 'TestShare'
# Shared path$Path = 'D:\SHARE'
# Remote host name$Server = 'Server'
If (!(Get-WmiObject -Class Win32_Share -Filter "name='$ShareName'")) 
{ 
	$Shares = [WMICLASS]"\\$Server\root\cimv2:WIN32_Share" 
	$($Path,$ShareName,0).ReturnValue
}
else
{
	Write-Warning "$ShareName has been sharing!!"
}

Low-key cmd

::It is recommended to view the current shared folder first and then create it
net share TestShare=D:\SHARE /users:25 /remark:"test share of the a folder"

We can easily turn on a folder's shared state, which we can access through the UNC path. After creating the file share, let’s see how to use it.

Drive mapping and shared access

Next, we abandon the operation of the graphical interface (if you don’t like it, you can view it through the online neighbor ["Network"], or right-click to select the map network drive under the computer icon), and let’s enable it through the command.

Powershell

Temporarily create a network drive map:

(New-Object -ComObject ).MapNetworkDrive("Z:", "\\TEST-PC\USERS")

Create a persistent network drive map:

# New-PSDrive plus the -Persist parameter makes the drive visible outside PowerShell.# To really create a permanent network drive, make sure to add -Scope Global.  /# If New-PSDrive is run outside the global scope (for example, in a script), the drive will only appear in the file manager when the script is running.New-PSDrive -Name Z -PSProvider FileSystem -Root \\TEST-PC\USERS -Persist -Scope Global

Small cmd

::Although the following command can becmdUse this disk map,But it is not loaded with the Explorer。
net use Z: \\TEST-PC\USERS
::Here issystemrootThe folder maps toz:Drive,Available""load,Unfortunately, you can't use the network path。
subst Z: $env:systemroot

After completing the above work, nothing unexpected happens, your Explorer will have an icon of the network path you want to access.

Delete share

If you no longer need to use this shared folder, you can uninstall the network drive and delete the share on the shared host.

Powershell

$Shares = Get-WMIObject Win32_Share | Where {$_.Name -eq ""}
Foreach ($Share in $Shares) {
   $()
}

cmd

net share TestShare /delete

summary

  • Creating a shared folder requires enabling network sharing and discovery in advance.
  • Folder permission control and sharing permission control need to be done in advance.
  • Identity authentication can be exempted by configuring cmdkey
cmdkey /add:targetname /user:username /pass:password

Extension: Setting up shared folders using PowerShell

Setting up a shared folder in PowerShell

# Define the folder path and share name to share$folderPath = "C:\Users\Public\Documents\SharedFolder"
$shareName = "SharedFolder"
# Create a shareNew-SmbShare -Name $shareName -Path $folderPath -FullAccess "Everyone"
# Show shared informationGet-SmbShare $shareName

Use the New-SmbShare command to create a share named SharedFolder and specify the path and full access to the shared folder (i.e., allow everyone to access). We then use the Get-SmbShare command to display the shared information.

It should be noted that to use the New-SmbShare and Get-SmbShare commands, you need to install Windows Management Framework 5.1 or later on your computer. In addition, you need to make sure you have permission to create shares on your computer and that the folder you want to share must exist.

This is the end of this article about folder sharing and disk mapping in Powershell. For more related Powershell disk mapping content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!