How to map a network drive with PowerShell
Note: The driveName does NOT have to be a letter it can be multiple characters, like DataDrive instead of Z.
param (
[String] $driveName = “Z”,
[String] $networkPath = “\\Localhost\c$”
)
$drive = “$($driveName):”
$driveExists = Test-Path “$($drive)”
If ($driveExists -eq $False) {
Write-Output “Map network drive $($drive) started”
New-PSDrive -Name $driveName -Root “$($networkPath)” -PSProvider FileSystem -Scope Global -Persist:$true
} else {
Write-Output “Network drive $($drive) exists”
}
Install a NPM package globally for all users on a Windows machine
Write-Host vs Write-Output vs Write-Information
If you want to log information form a PowerShell script and you want to ouput it to the default output stream, but also to the “Information” output stream, you can use Write-Host, instead of Write-Output or Write-Information
I use Write-Host, when the script was executed by a .NET Core application, to see the progress of the PowerShell script during execution.
Execute a PowerShell script with PowerShell 7
When you want to execute a PowerShell script with the PowerShell 7 executable, you can use a link in the same folder as the script without setting the “Start in” property.
Just put this in the target: “C:\Program Files\PowerShell\7\pwsh.exe” -NoExit “.\my-script.ps1”
Now you can easily share the *.lnk file.