Menu
CCMEXEC.COM – Enterprise Mobility
  • Home
  • General
  • Configuration Manager
  • Windows 10
  • Windows 11
  • Intune
  • GitHub
  • About
CCMEXEC.COM – Enterprise Mobility

Map drives when connecting to corporate network

Posted on November 16, 2020November 17, 2020 by Jörgen Nilsson

In every modern management project where we use Azure AD Join instead of traditional domain join, there are always some network drives that needs to be mapped for the end users. Not very modern I know but there is a real world out there as well.
When we use a modern client using sleep or hibernate is the new way to work. Running a script at logon isn’t enough we need to be more flexible.
There are a great number of great logon script samples out there so I will not go down that way. Instead adding a trigger to a scheduled task to run when we connect to a network with a specific name, is a useful addition which makes the end-user experience much better.

Here is a short demo on how to map drives when connecting to corporate network.

But wait! No blue PowerShell splash screen was in the video.
I use PSRun.exe that my colleague Johan Schrewelius has written which you can find here: https://onevinn.schrewelius.it/Apps01.html. PSRun suppresses the PowerShell splash screen and passes all commands that you execute PSRun with directly to PowerShell.

The action we use in the schedule task looks like below. The arguments line can hardly be read in the screenshot but it looks like this:
-executionpolicy Bypass -file “C:\Program Files\ConnectDrives\ConnectDrives.ps1”

Action

Our Schedule Task have two triggers, one to run at logon and one custom that runs when connected to a network with a specific name. It looks like this:

Schedule Task triggers

The custom event filter contains the network name for which we will use to trigger the script when connected to.

Custom event filter

The scripts

I wrote a simple sample script to import the schedule task and copy the simple script I used to map the drives to C:\Program Files\ConnectDrives. It also writes to the registry so there is a registry key and value. Which can be used as a detection method when deploying it using Win32App on our modern clients.

<#
    Name: Install.ps1 
    Version: 1.0
    Author: Jörgen Nilsson
    Date: 2020-11-15
#>

[string]$RegKeyName = "ConnectDrives"
[string]$FullRegKeyName = "HKLM:\SOFTWARE\ccmexec\" + $regkeyname 
[string]$InstallPath = "$env:ProgramFiles\ConnectDrives"

# Create registry value if it doesn't exist
If (!(Test-Path $FullRegKeyName)) {
    New-Item -Path $FullRegKeyName -type Directory -force 
    New-itemproperty $FullRegKeyName -Name "Connectdrives" -Value "1" -Type STRING -Force
    }
If (!(Test-Path $InstallPath)) {
    New-Item -Path $InstallPath -type Directory -force 
    }

Copy-Item -Path "$PSScriptRoot\ConnectDrives.ps1" -Destination $InstallPath -Recurse -Force
Copy-Item -Path "$PSScriptRoot\psrun.exe" -Destination $InstallPath -Recurse -Force

# Creates ScheduleTask
Register-ScheduledTask -Xml (get-content $PSScriptRoot\ConnectDrives.xml | out-string) -TaskName "ConnectDrives"

The script I used to map the drives:

If (!(Test-Path G:)) {
   New-PSDrive G -PSProvider FileSystem -Persist -Root "\\d00001\share"
}

If (!(Test-Path M:)) {
   New-PSDrive M -PSProvider FileSystem -Persist -Root "\\d00001\sources"
}

Registry key/value configured by the script:

Registry key an value

The files in the script:

Files in the install folder

The scripts can be downloaded here: https://github.com/Ccmexec/Intune-MEM

PSRun needs to be downloaded from here and copied to the folder:
https://onevinn.schrewelius.it/Apps01.html

I hope this is useful!

1 thought on “Map drives when connecting to corporate network”

  1. Pingback: Switch to Private Firewall profile on AAD joined when connected to specific network. - CCMEXEC.COM - Enterprise Mobility

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

My name is Jörgen Nilsson and I work as a Senior Consultant at Onevinn in Malmö, Sweden. This is my blog where I will share tips and stuff for my own and everyone elses use on Enterprise Mobility and Windows related topics.
All code is provided "AS-IS" with no warranties.

Recent Posts

  • New settings in Intune Security Baseline Windows 11 24H2 -2504
  • Managing extensions in Visual Studio Code
  • Reinstall a required Win32app using remediation on demand
  • Administrator protection in Windows 11 – First look
  • Remediation on demand script – ResetWindowsUpdate
©2025 CCMEXEC.COM – Enterprise Mobility | WordPress Theme by Superb Themes
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT