Map Drive on GPO with powershell

  Create Folder for Script

mkdir c:\Scripts
Copied!

  Share this Folder

New-SmbShare `
-Name "Scripts" `
-Path "c:\Scripts" `
-ReadAccess "everyone"
Copied!

  Enable permission

icacls "C:\Scripts" /grant "*S-1-1-0:(OI)(CI)(M)"
Copied!

  Create the logon script

Set-Content -Path C:\Scripts\mapDrive.bat `
    -Value 'net use U: \\server\Scripts /persistent:yes' `
    -Encoding ASCII
Copied!

  Verify the script

type C:\Scripts\mapDrive.bat
Copied!

  Load the Group Policy module

Import-Module GroupPolicy
Copied!

  Create the GPO for "Map Drive"

New-GPO -Name "Map Drive"
Copied!

  Link the GPO to the correct OU

New-GPLink `
-Name "Map Drive" `
-Target "OU=Team,DC=server,DC=kh"
Copied!

  Add the Logon Script to GPO "Map Drive"

$gpo = Get-GPO "Map Drive"

$guid = $gpo.Id

$path = "\\server.kh\SYSVOL\server.kh\Policies\{$guid}\User\Scripts"

New-Item "$path\Logon" -ItemType Directory -Force

Copy-Item "C:\Scripts\mapDrive.bat" "$path\Logon"

@"
[Logon]
0CmdLine=mapDrive.bat
0Parameters=
"@ | Set-Content "$path\Scripts.ini"
Copied!