Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language. PowerShell enables administrators to perform administrative tasks on both local and remote Windows systems.
For many people it is simply a replacement for the familiar Command Prompt and it is included with Windows 7.
The following information explains how you can list and create network shares using Powershell. I use the script to create shares on drives that are not mounted until after Windows has booted.
List all the shares on this computer :
Get-WmiObject Win32_Share
Get-WmiObject Win32_Share | sort type, name
Get-WmiObject Win32_Share -computerName Name
$Shares = [wmiClass] 'Win32_share'
$Shares.create("E:\My Data", "My Data", 0)
Function CreateShare($Foldername,$Sharename) {
#
# This function creates a network share
#
# $Foldername - The full path to the share
# $Sharename - The share name
write-host "########## $Sharename ##########"
# Check if the path exists
IF (!(TEST-PATH $Foldername)) {
# Create folder
NEW-ITEM $Foldername -type Directory
write-host "Created $Foldername"
}
# Create Share but check to make sure it isn’t already there
If (!(GET-WMIOBJECT Win32_Share | Where-Object -FilterScript {$_.Name -eq $Sharename})) {
# Create share
$Shares = [wmiclass]"Win32_Share"
$results = $Shares.Create($Foldername,$Sharename,0,0)
if ($results.returnvalue=0) {
# Share created ok
write-host "Created $Sharename"
} else {
# Error creating share
write-host "Failed to create $Sharename ERROR:" $results.returnvalue
}
} ELSE {
# Share name already exists
write-host "$Sharename already exists"
}
write-host ""
}
CreateShare "E:\My Data\" "My Data"