Zum Hauptinhalt springen

Windows Freigaben auf einen neuen Pfad migrieren mit Powershell

Wenn auf einem Dateiserver Ordner verschoben werden müssen, kann das Anpassen der Freigaben für darunterliegende Verzeichnisse sehr aufwändig sein — besonders bei vielen Home-Shares. Statt die GUI zu verwenden, lässt sich das schneller erledigen, indem die Registry-Einträge unter:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Shares

angepasst werden. Alternativ kannst du die folgende PowerShell-Funktion verwenden, die einen angegebenen Pfad im Registry-Schlüssel durch einen neuen ersetzt; da sie rein nach Muster ersetzt, lassen sich beliebige Teilpfade austauschen.

function Replace-SharePath {
    <#
    .SYNOPSIS
    Replace the Filesystem-Path of a Share

    .DESCRIPTION
    Replace-SharePath can change the Filesystem-Path of shares. This can be helpful if you need to move files to a new drive or have to move files to a subfolder and you have multiple shares to touch. The script will change the given string into the new string, so all shares under the given path will be affected.

    .EXAMPLE
    To move files from C:\Shares to D:\NewSharesFolder:
    Replace-SharePath -Path C:\Shares -NewPath D:\NewSharesFolder
    #>

    param(
        [ValidateScript({ if (!(Test-Path -Path $_ -PathType Container)) { throw "Bitte den Quellpfad prüfen!" }; $true })]
        [Parameter(Mandatory = $true)]
        $Path,

        [ValidateScript({ if (!(Test-Path -Path $_ -PathType Container)) { throw "Bitte den Quellpfad prüfen!" }; $true })]
        [Parameter(Mandatory = $true)]
        $NewPath
    )

    $Path = $Path.Replace('\\', '\\\\')
    $NewPath = $NewPath.Replace('\\', '\\\\')
    $ShareList = Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Shares'

    foreach ($Share in $ShareList.Property) {
        Get-ItemProperty -Path $ShareList.PSPath -Name $Share
        $newValue = (Get-ItemProperty -Path $ShareList.PSPath -Name $Share).$Share -replace "(^Path=)($Path)", "Path=$NewPath"
        Set-ItemProperty -Path $ShareList.PSPath -Name $Share -Value $newValue -PassThru
    }
}

Führen Sie die Funktion einmal aus und starten Sie sie anschließend über folgende Eingabe:

Replace-SharePath -Path 'C:\OldSharePath' -NewPath 'D:\NewSharePath'

Damit lassen sich Windows-Freigaben relativ einfach migrieren.

Praxis-Tipp aus unserem Trainingscenter: Wollen Sie Ihre IT-Infrastruktur professionell automatisieren? Entdecken Sie unsere modularen Seminare für Einsteiger und Profis: PowerShell Schulung in Hannover.

×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Already Registered? Login Here
Donnerstag, 17. September 2026