Permanently mapping network drive for all users?

PrzemekJ
PrzemekJ Member Posts: 1
edited June 2023 in Scripts

Has anyone figured out a PS script (or bat etc) that would permanently map a password protected network drive for all users of a remote workstation?

I've tried a couple of approaches - New-SMBMapping , New-SmbGlobalMapping , New-PSDrive but I can't figure it out. At best I was able to map a network share, but it was gone after reboot.

Tagged:

Comments

  • tanderson
    tanderson Member Posts: 271 ✭✭✭✭

    @PrzemekJ To map a network drive with PowerShell, you can use the New-PSDrive cmdlet. However, the drive mapping created with New-PSDrive only lasts for the duration of the PowerShell session. If you want the mapping to persist across reboots and for all users, you would traditionally use the net use command with the /persistent:yes option, but this will only apply to the currently logged-in user, not all users.

    To make a persistent network drive mapping that applies to all remote workstation users, you would need to create a script and ensure it runs at startup for each user. This would require administrative privileges on the target workstation.

    However, storing passwords in a script is not considered a good security practice as it can lead to potential security issues. Consider using network drives that don't require individual user passwords or find a secure credential storage and retrieval method if possible.

    Here is a general example of what the PowerShell script might look like:


    $Username = "username"

    $Password = ConvertTo-SecureString "password" -AsPlainText -Force

    $Credential = New-Object System.Management.Automation.PSCredential($Username, $Password)

    $DriveLetter = "Z:"

    $NetworkPath = "\\server\share"

    $Persist = $true

    New-PSDrive -Name $DriveLetter -PSProvider FileSystem -Root $NetworkPath -Persist $Persist -Credential $Credential


    In this example, replace "username" and "password" with the network drive's username and password. Replace "Z:" with the drive letter, you want to assign to the network drive and replace "\\server\share" with the path to the network drive.

    To run this script at startup for each user, you must place it in the shared startup folder. The standard startup folder for all users in Windows is typically located at C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp.

    But remember, the method described above isn't secure because the password isn't encrypted or secured. It's generally not recommended to store passwords in plain text. Instead, you should look for ways to securely store the credentials or use a system that doesn't require per-user network drive authentication.

  • nina
    nina Administrator Posts: 428 admin

    Thanks @tanderson !