Skip to content

Mike Simmons

My Workbench

  • Home
  • LinkedIn
  • Home
  • Windows
  • Powershell script to create user home directory and DFS namespace target

Powershell script to create user home directory and DFS namespace target

Posted on March 16, 2018March 16, 2018 By Mike No Comments on Powershell script to create user home directory and DFS namespace target
Active Directory, PowerShell, Windows

Creating a new users home directory and corresponding DFS namespace target. The script can also force all domain controllers to fully sync up in case you have multiple sites. Sync is enabled by default, however it can be disabled. This powershell script does require WinRM to be enabled for sync to work. You need to have setup the DFS namespace root path already.


CreateHome -user $target -server $server -dfsNamespacePath "\\example.com\UserFolders" -sync $true

Function CreateHome {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True,Position=0)]
[string]$user,
[Parameter(Mandatory=$True,Position=1)]
[string]$server,
[Parameter(Mandatory=$True,Position=2)]
[string]$dfsNamespacePath,
[Parameter(Mandatory=$False,Position=3)]
[bool]$sync = $true
)
if ($sync)
{
write-host "Syncing Changes..."

$dcs = Get-ADComputer -Filter * -Properties * | Where-Object { $_.CanonicalName.contains('/Domain Controllers/', "CurrentCultureIgnoreCase") }
$dcs | ForEach-Object {
Write-Host "Syncing $($_.Name)"
$res = Invoke-Command -ComputerName $_.Name -ScriptBlock { repadmin /syncall /AdeP }
write-host "Syncing Complete..."
}
}

trap {continue} New-Item -Path "$server\$user" -type Directory -ErrorAction SilentlyContinue

$Path = "$server\$user"
$acl = (Get-Item $Path).GetAccessControl('Access')
$allInherit = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit", "ObjectInherit"
$allPropagation = [System.Security.AccessControl.PropagationFlags]"None"
$permissions = "FullControl"
$Username = "$user"
$AR = New-Object System.Security.AccessControl.FileSystemAccessRule($Username, $permissions, $allInherit, $allPropagation, "Allow")
if ($acl.Access | Where { $_.IdentityReference -eq $Username})
{
$accessModification = New-Object System.Security.AccessControl.AccessControlModification
$accessModification.value__ = 2
$modification = $false
$acl.ModifyAccessRule($accessModification, $AR, [ref]$modification) | Out-Null
}
else
{
$acl.AddAccessRule($AR)
}
Set-Acl -path $Path -AclObject $Acl

$result = dfsutil link add "$dfsNamespacePath\$user" "$server\$user"
}

Tags: active directory dfs home folder POWERSHELL

Post navigation

❮ Previous Post: C# save DataTable to CSV easily
Next Post: Open Maintenance – Docker & CentOS 7 ❯

Leave a Reply Cancel reply

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

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.

Pages

  • LinkedIn

Recent Posts

  • PowerShell Anti-Idle Script
  • WSUS MMC Crash Due To XML Invalid Character!
  • Open Maintenance – Docker & CentOS 7
  • Powershell script to create user home directory and DFS namespace target
  • C# save DataTable to CSV easily

Recent Comments

    Archives

    • August 2022
    • March 2018
    • July 2017
    • August 2015

    Categories

    • Active Directory
    • C#
    • CentOS
    • Docker
    • GrayLog
    • MSSQL
    • Networking
    • PowerShell
    • Programming
    • Uncategorized
    • Windows
    • XML

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org

    Copyright © 2023 Mike Simmons.

    Theme: Oceanly by ScriptsTown