What It Does
For the user signed in at the console, the script maps the drives in that user's own session, with that user's own rights. For every item in Mappings it leaves a drive letter alone when it is already mapped to the same path and opens, and otherwise maps the letter to the share with net use. With Persistent true the mapping is saved in the user's profile and comes back at every sign-in.
It refuses the whole run, before changing anything, when a letter is already mapped to a different path, unless Replace is true, in which case that mapping is removed and replaced. It also refuses when a letter is a local disk or other non-network drive, such as C: or a virtual disk. It then checks that each drive opens, and for persistent drives that the mapping is saved in the user's registry.
It uses the signed-in user's own credentials only. No user name or password is taken or stored, so the user needs access to the share. It maps the drives of the user signed in at the console only, never other users, and never touches files on the share. If a share cannot be reached, the run ends in an error naming the drive and the Windows message. Nobody signed in is SKIPPED.
Mapping by group membership or per site is not covered. Do that in Group Policy or Intune, or run the script with different Mappings per group in your RMM. Shares that need a different user name are not covered either.
Before You Run It
- Check the user has access to each share, and that the PC can reach the server, on the network or VPN.
- Use Replace only when you mean to move a letter to a different share.
- Use DryRun=true to list what would change before running it for real.
- For group based drive maps, use Group Policy or Intune instead.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: Low. Set DryRun=true, or run it with -WhatIf, to list every change without making any.
- What it changes: Maps network drive letters for the signed-in user (saved in their profile when Persistent); with Replace, removes a drive mapped to a different path.
- Restart: It never restarts the machine.
- Typical run: Under 1 minute.
- Example answer: "Mapped 2 drive(s) for PC01\jsmith: S: H: (persistent)"
- Tested before listing on Windows 11 with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- Mappings: The drives to map: items like Z:=\\server\share, separated by commas or semicolons. The share may include a sub-folder (Z:=\\server\share\folder). Required.
- Persistent: true (default) to keep the mappings across sign-ins; false to map them for this sign-in only.
- Replace: true to replace a drive letter that is mapped to a different path. Default false: such a letter ends the run in an error and nothing is changed.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.