PowerShell Scripts, Tips, Expert Advices, Forums, and Resources

image rotation

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.

Latest Scripts

10-28-2014 - Get-FileTail
10-26-2014 - Yet another way to get mime type
10-26-2014 - Different ways to find os version

As a Powershell.com member you will have access to:

  • Daily PowerShell tips written by Microsoft MVPs and other leading Windows PowerShell experts
  • Free Windows PowerShell advice and training provided by Microsoft MVPs and other leading Windows PowerShell experts
  • Access to leading Windows PowerShell blogs
  • A free ebook, Mastering PowerShell, written by Microsoft MVP Dr. Tobias Weltner
PowerTip of the Day

Creating New Shares

All PowerShell versions

WMI can easily create new shares. Here is sample code that will create a local share:

$ShareName = 'NewShare'
$Path = 'c:\123'

If (!(Get-WmiObject -Class Win32_Share -Filter "name='$ShareName'")) 
{ 
  $Shares=[WMICLASS]"WIN32_Share" 
  $Shares.Create($Path,$ShareName,0).ReturnValue
}
else
{
  Write-Warning "Share $ShareName exists already."
}

You can also create shares on remote machines, provided you have Admin privileges on the remote machine. To do that, simply add the complete WMI path like this:

$ShareName = 'NewShare'
$Path = 'c:\123'
$Server = 'MyServer'

If (!(Get-WmiObject -Class Win32_Share -Filter "name='$ShareName'" -ComputerName $Server)) 
{ 
  $Shares=[WMICLASS]"\\$Server\root\cimv2:WIN32_Share" 
  $Shares.Create($Path,$ShareName,0).ReturnValue
}
else
{
  Write-Warning "Share $ShareName exists already."
}

Twitter This Tip! ReTweet this Tip!

Copyright 2012 PowerShell.com. All rights reserved.