Creating a Post Logon script and GPO using PowerShell to map a network share drive

Tyrik Emptage
4 min readJul 7, 2024

--

This is a follow up to my previous blog about some features in Windows Server. These steps are referenced from a Youtube video made by Cobuman. This is Part 2 of 2 of this mini-project I worked on.

Prerequisites

  • Virtualization platform such as VirtualBox or VMWare
  • Hardware requirements to run a Virtual Machine (VM) such as CPU or RAM.
  • Windows Server installation ISO file
  • Admin access to the host machine

Procedure

Creating a PowerShell script in the Windows Server VM

I went to Windows PowerShell ISE and ran the app as admin. On the bottom blue screen, I tested out the following commands:

1) The “Get-PSDrive” command was used to list all mapped drives.

2) Then, I used this “net use” command on a drive that wasn’t listed (ex: J drive) to verify if this drive could be mapped on the network and stay persistent (or remain on the computer for other users).

net use J: \\server\newshared /persistent:yes

Also, I tested out a similar command for New-PSDrive (shown below) which can also be used to verify if the drive mapping was completed.

New-PSDrive -Name Z -PSProvider FileSystem -Root “\\server\newshared” -Persist

After clicking Enter, it says the command was completed successfully. Note: If you open File Explorer after this, a network location will appear for a mapped drive.

Then, I copied and pasted the line that was highlighted into the script. Click on the new script icon if it doesn’t open in PowerShell ISE. Save that line of script in a folder (ex: PS Scripts) for later.

Creating the Group Policy Object (GPO) for the Logon script

I went to the Search bar and looked for Group Policy Management in the Windows Server VM. Then, I right clicked the Finance OU that was made under CORP, clicked “Create a GPO and Link it here”. Add the name of your GPO (ex: Finance J Drive Mapping) and click OK. Then, right-click the GPO and select Edit.

Since I am modifying when a user logs into a computer, I did the following:

Go to User Configuration -> Policies -> Windows Settings -> Scripts -> Logon -> PowerShell scripts -> Add -> Browse.

Then, copy the PowerShell script that was saved, paste it into the Logon path and click Open, Apply, and OK. Close and reopen group policy management.

Check and verify that the script was added by clicking the new GPO -> settings and scroll-down to see the given name of the script (ex: Finance J Drive).

Testing the Post Logon Script

For this example, I created a new user named “Steph” who I added onto the Finance OU.

Note: I noticed when doing the user configuration for the GPO that the script didn’t work if the user was in a Global group such as “Finance Department H drive”, so I moved the user in the Finance OU only.

I restarted the Windows 10 VM and signed into Steph’s user account.

After opening “This PC” in File Explorer, I noticed the J drive was mapped when logging in.

The same text document that I created was in both the Windows Server VM and Windows 10 VM.

Link to Part 1: https://medium.com/@temptage01/creating-a-shared-network-drive-automated-backup-drive-folder-sharing-permissions-and-mapping-a-175c3c3b08ae

Let me know what feedback you have for these articles.

--

--