sthildas Posted November 30, 2016 Posted November 30, 2016 Hi I am tryng to create a script that will move all our users HomeFolders to a subfolder called "Documents". It will create the documents folder if it does not exist and then move the contents of the users home folder to the documents folder. It will then create an "Autorecovery" folder as a sub folder of "Documents". To test this I have removed the option to carry out the move task and I am first trying to get the script to test if the "Documents" folder already exist and if not then it should create the folder. The test should output a write-host that either says the document does or doesnt exist with the user name. When I run this and look at the users that the script says have a documents folder, some do and some dont. The same goes for the ones that the script says dont have a documents folder. $userhomes = Get-ChildItem "D:" | where {$_.Attributes -like '*Directory*'} foreach($userhome in $userhomes) { $exclude = "Documents" $movesource = "D:" + $userhome.Name $movedest = "D:"+ $userhome.Name +"\Documents" $autodest = "$movedest\Autorecovery" $Search = "OU=Users,DC=domain,DC=com" $Users = Get-ADUser -Filter * -SearchBase $Search $users | % { # Test if Documents folder exists, create folder if it doesnt exist If (Test-Path $movedest) { Write-Host Documents folder exists for ($_.SamAccountName) -ForegroundColor Red } else { # Create the Documents folder # Get-Item -path $movesource | New-Item -ItemType Directory -Path "$movesource\Documents" Write-Host Documents folder being created for ($_.SamAccountName) -ForegroundColor Green } # Test if Autorecovery folder exists, create folder if it doesnt exist If (Test-Path $autodest) { Write-Host Documents and Autorecovery folder exist for ($_.SamAccountName) -ForegroundColor Red } else { # Create the Autorecovery folder and set hidden attribute # Get-Item -path $movesource | New-Item -ItemType Directory -Path "$movesource\Documents\Autorecovery" | %{$_.Attributes="hidden"} Write-Host Autorecovery folder being created for ($_.SamAccountName) -ForegroundColor Green } # Move Documents to new location if Documents folder exists If (Test-Path $autodest) { # Get-Childitem -path $movesource -exclude $exclude | Move-Item -Dest $movedest Write-Host Documents being moved for ($_.SamAccountName) -ForegroundColor Green # Set user new home folder path Get-ADUser -Filter * -SearchBase $Search | Foreach-Object{ $sam = $_.SamAccountName Set-ADuser -Identity $_ -HomeDrive "H:" -HomeDirectory "\\server\share\$sam\Documents" } else { Write-Host Autorecovery folder does not exist for ($_.SamAccountName) -ForegroundColor Red } } } }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now