PowerShell Create Shared Folder

 # Set up an existing folder as a shared folder; shares permissions set to full access for everyone.
             
        
        #------------------ Create Hidden Home Folder Share  -----------
        # Username/Group to give permissions to full access to the everyone
        # Controlled access at the NTFS level.
        

        $ServerName="MyServerName"
        $trustee = ([wmiclass]'Win32_trustee').psbase.CreateInstance()
        $trustee.Domain = "$ServerName"
        $trustee.Name = "Everyone"

        
        
        #---------------Set Hidden Share Name---------------------------

        $SAM="USERS-SAM-ID"
        $ShareName = "$SAM" + "$"

        
        
        #-------------------Set Accessmask values-----------------------

        $fullcontrol = 2032127
        $change = 1245631
        $read = 1179785

        
        #------------------Create access-list---------------------------

        $ace = ([wmiclass]'Win32_ACE').psbase.CreateInstance()
        $ace.AccessMask = $fullcontrol
        $ace.AceFlags = 3
        $ace.AceType = 0
        $ace.Trustee = $trustee

        
        #-----------------Securitydescriptor containting access---------

        $sd = ([wmiclass]'Win32_SecurityDescriptor').psbase.CreateInstance()
        $sd.ControlFlags = 4
        $sd.DACL = $ace
        $sd.group = $trustee
        $sd.owner = $trustee

        
        
     
  $share = Get-WmiObject Win32_Share -ComputerName $ServerName -List
        
        
        #----------- Set the Share Path on Local File System ------------
       
$SharePath = "C:\RootFolderShare\$SAM"
        
        #----------------- Creates the Share on The Server---------------
     
  $share.create($SharePath, $ShareName, 0, $Null, "", "", $sd)   

2020 | Scott W. Head  |  Sr. Systems Admin | Certified Microsoft Windows Server Administrator