Beta 42

Research and Development

Menu

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. Note 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.