#Backstory: we had a set of home folder shares on a server that we needed to migrate to a new server, DFS allowed us to replicate the content and NTFS #permissions but not the share properties on the folders. wrote the script below to accomplish re-applying the share properties

#####################################################

#applies share permissions to leaf folders that are in username format

#changes ad profile home directory to new location

#folders must end in name that matches the username..

######################################################

$ErrorActionPreference = "Stop"

#get shares in specific folder, i.e. \\server\e$\1,2,3 or whatever it is

Get-ChildItem \\location\offolderstobeshared |

?{ $_.PSIsContainer } | 

Select-Object FullName | 

export-csv -delimiter "`t" -path c:\scripts\SharesQ.txt


#cleanup

get-content c:\scripts\SharesQ.txt |

select-string -pattern '#TYPE Selected.System.IO.DirectoryInfo' -notmatch |

select-string -pattern 'FullName' -notmatch | % {$_ -replace '"', ""} | 

out-file c:\scripts\Shares.txt


#might not need to dump these to files but eh can see whats happening


$Shares = Get-Content c:\scripts\Shares.txt

foreach($share in $Shares) 

{

try{

$client = Split-Path "$share" -leaf

$HideShare = [string]::concat($client,"$")


NET SHARE $HideShare=$share "/GRANT:$client,FULL" 


Get-ADUser -Filter {cn -like "$client"} -ErrorAction Stop

Set-ADuser -Identity $client -HomeDrive "H:" -HomeDirectory "\\NEWSERVER\$hideshare"

}

catch

{

if ($lastexitcode -ne 0) {"$Client" | out-file 'c:\scripts\bad_names.TXT' -Append}

#Collect list of each folder that was unable to be shared due to missing user etc

}

}