Adding Profile paths

Adding Profile paths

Use this code

NOTE: Change things like paths and domains.

Import-Module ActiveDirectory
 
# Fileserver + share

$profileShare = "\\dc-01\Profiless$"
 
# Basis OU (alle afdelingen zitten hier onder)

$searchBase = "OU=PBC,DC=PBC,DC=nl"
 
# Alle gebruikers onder PBC ophalen

$users = Get-ADUser -SearchBase $searchBase -Filter *
 
foreach ($user in $users) {

    $username = $user.SamAccountName

    $profilePath = "$profileShare\$username"

    $localPath = "\\dc-01\Profiless$\$username"
 
    Write-Host "Verwerk gebruiker: $username" -ForegroundColor Cyan
 
    # Profielpad in AD instellen

    try {

        Set-ADUser $user -ProfilePath $profilePath -ErrorAction Stop

        Write-Host " → Profielpad ingesteld: $profilePath" -ForegroundColor Green

    } catch {

        Write-Host (" ! Kon profielpad niet instellen voor {0}: {1}" -f $username, $_.Exception.Message) -ForegroundColor Red

        continue

    }
 
    # Map aanmaken als die nog niet bestaat

    if (-not (Test-Path $localPath)) {

        try {

            New-Item -Path $localPath -ItemType Directory | Out-Null

            Write-Host " → Map aangemaakt: $localPath" -ForegroundColor Yellow
 
            # NTFS rechten instellen (alleen gebruiker + admins)

            $acl = Get-Acl $localPath

            $ruleUser = New-Object System.Security.AccessControl.FileSystemAccessRule(

                $username, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow"

            )

            $ruleAdmins = New-Object System.Security.AccessControl.FileSystemAccessRule(

                "DOMAIN ADMINS", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow"

            )

            $ruleSystem = New-Object System.Security.AccessControl.FileSystemAccessRule(

                "SYSTEM", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow"

            )
 
            $acl.SetAccessRuleProtection($true, $false) # Overerving uitzetten

            $acl.ResetAccessRule($ruleUser)

            $acl.AddAccessRule($ruleAdmins)

            $acl.AddAccessRule($ruleSystem)

            Set-Acl $localPath $acl
 
            Write-Host " → NTFS rechten ingesteld voor $username" -ForegroundColor Green

        } catch {

            Write-Host (" ! Kon map of rechten niet instellen voor {0}: {1}" -f $username, $_.Exception.Message) -ForegroundColor Red

        }

    } else {

        Write-Host " → Map bestaat al: $localPath" -ForegroundColor Gray

    }

}

NOTE: if the script doesn't work you might need to change the path to your local path