$scriptPath = "$env:USERPROFILE\Scripts\MapNetworkDrives.ps1" @" # Delay to allow network to initialize Start-Sleep -Seconds 10 # Import credential (DPAPI encrypted XML) $cred = Import-Clixml -Path "$env:USERPROFILE\Scripts\netcreds.xml" # Create (or update) persistent mapped drive(s) # Example: map Z: to \\fileserver\share $driveLetter = 'Z' $remotePath = '\\fileserver\share' # Remove existing mapping if present (optional) if (Test-Path -Path "$driveLetter`:") { Remove-PSDrive -Name $driveLetter -Force -ErrorAction SilentlyContinue } New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root $remotePath -Credential $cred -Persist -ErrorAction Stop "@ | Out-File -FilePath $scriptPath -Encoding UTF8 -Force