Home > Blockchain >  PowerShell Script To Map OneDrive as Drive
PowerShell Script To Map OneDrive as Drive

Time:10-06

I'm trying to make me a small bat which will execute a powershell command.

powershell "New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name OneDriveMapping -Value "subst T: ""$($env:OneDrive)\General\srv\db""" -PropertyType String -Force | Out-Null""

Which will link OneDrive\General\srv\db as T: drive, but I'm receiving error :

powershell : New-ItemProperty : A positional parameter cannot be found that accepts argument 'T:'.

I believe is caused by syntax " ', but cannot resolve it. Can somebody give a small tip.

I do not want to use PowerShell Script because ps1 is restricted in my org, therefor I try to have one powershell command which will create the registry key.

Original Source Code :

   [Parameter(Mandatory=$true)]
   [string]$folderName,
   [Parameter(Mandatory=$true)]
   [string]$driveLetter = "Z:\"
)

New-Item "$($env:OneDrive)\$folderName" -ItemType Directory -force
$command = "subst $driveLetter ""$($env:OneDrive)\$folderName"""
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name OneDriveMapping -Value $command -PropertyType String -Force | Out-Null```
  
 

 

CodePudding user response:

In cmd.exe you can escape quotes by using """
In powershell you can escape quotes by using backticks
When you enter this in cmd: powershell "Write-Host """Test""""
This will be send to powershell: Write-Host "Test"
So for your subst command you will need this:

"""subst T: `"""$($env:OneDrive)\General\srv\db`""""""

So your entire command should look like this:

powershell "New-ItemProperty -Path """HKCU:\Software\Microsoft\Windows\CurrentVersion\Run""" -Name OneDriveMapping -Value """subst T: `"""$($env:OneDrive)\General\srv\db`"""""" -PropertyType String -Force | Out-Null"
  • Related