PowerShell: Sharing Folders

Console commands are first class PowerShell citizens, so sometimes it may be easier to use classic console commands to solve a problem. Here is a function that creates a local folder and also shares it so others can use it via network. Just be aware that the net.exe used to share the folder requires you to have full admin privileges:

function New-Share {
    param($Path, $Name)

    try {
        $ErrorActionPreference = 'Stop'

        if ( (Test-Path $Path) -eq $false) {
            $null = New-Item -Path $Path -ItemType Directory
        } 
        net share $Name=$Path
    }
    catch {
        Write-Warning "Create Share: Failed, $_"
    }
}

And this is how you share new folders:

PS> New-Share c:\myfolder sharedplace

sharedplace was shared successfully.

No comments yet.

Leave a Reply

revia in alcoholism