Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joegasper/31adcf16f38434f772cb9e105969fe10 to your computer and use it in GitHub Desktop.
Save joegasper/31adcf16f38434f772cb9e105969fe10 to your computer and use it in GitHub Desktop.
Create Windows persistent drive substitute with support for Recycle Bin ( subst psubst )
# Run elevated. Reboot is required.
# Enter your drive substitute and path
$DriveLetter = 'D'
$MapPath = 'C:\GitHub'
# Inspired by https://superuser.com/questions/29072/how-to-make-subst-mapping-persistent-across-reboots
# Do not edit below
# Setup GUID in standard Registry notation
$GUID = '{' + [guid]::NewGuid().ToString() + '}'
# Setup Registry locations
$RegPath0 = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices'
$RegPath1 = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions'
$RegPath2 = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions'
# Create new Registry keys based on the GUID
New-Item -Path $RegPath1 -Name $GUID -Force
New-Item -Path $RegPath2 -Name $GUID -Force
# Add peristent substitute drive
New-ItemProperty -Name "$($DriveLetter):" -PropertyType String -Value "\??\$MapPath".TrimEnd('\') -Path $RegPath0
# Enable Recycle Bin for substitute drive
New-ItemProperty -Name 'RelativePath' -PropertyType String -Value "$($DriveLetter):\" -Path "$RegPath1\$GUID","$RegPath2\$GUID"
New-ItemProperty -Name 'Category' -PropertyType Dword -Value 4 -Path "$RegPath1\$GUID","$RegPath2\$GUID"
New-ItemProperty -Name 'Name' -PropertyType String -Value "$($DriveLetter)_Drive" -Path "$RegPath1\$GUID","$RegPath2\$GUID"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment