Jump to content

Recommended Posts

Posted (edited)

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

}

}

}

}

Edited by sthildas
No code added
Posted

This is what we used before, obviously need to change the paths to whatever you need:

 

 @echo off
FOR /d %%i in ("*") DO (
echo 1 - We found %%i 
Robocopy Y:\StudentHomeDrives2\14\%%i Y:\StudentHomeDrives2\14\%%i\Documents /MOVE /S /R:2 /W:1 /XD Documents 


)

 

Will move all the files/folders from the users top folder into a Documents folder, excluding any folder called Documents that's there. Obviously might want to run a small test first on your envirnment :p

 

Steve

Posted (edited)

Eventually got this working

 

 

$userhomes = Get-ChildItem "D:" | where {$_.Attributes -like '*Directory*'}

foreach($userhome in $userhomes) {

$exclude = "Documents"

$movesource = "D:" + $userhome.Name

$movedest = "D:"+ $userhome.Name +"\Documents"

$autodest = "D:"+ $userhome.Name +"\Documents"+"\Autorecovery"

$testpath = "D:"+ $userhome.Name +"\Desktop"

$Search = "OU=Users,DC=domain,DC=co,DC=uk"

$Users = Get-ADUser -Filter * -SearchBase $Search

 

# Test if Documents folder exists, create folder if it doesnt exist

If (Test-Path $movedest)

{

Write-Host Documents folder exists for $($userhome)

 

# Test if Autorecovery folder exists, create folder if it doesnt exist

If (Test-Path $autodest)

{

Write-Host Autorecovery folder exist for $($userhome)

 

# Move Documents to new location

If (Test-Path $testpath)

{

Get-Childitem -path $movesource -exclude $exclude | Move-Item -Dest $movedest

Get-ChildItem -Path $movesource -Exclude "Documents" | ? {$_.PSIsContainer} |Remove-Item -Recurse -force

Write-Host Documents being moved for $($userhome) -ForegroundColor Yellow

}

else

{

Write-Host Documents already moved for $($userhome)

}

}

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 $($userhome) -ForegroundColor Green

 

# Move Documents to new location

If (Test-Path $testpath)

{

Get-Childitem -path $movesource -exclude $exclude | Move-Item -Dest $movedest

Get-ChildItem -Path $movesource -Exclude "Documents" | ? {$_.PSIsContainer} |Remove-Item -Recurse -force

Write-Host Documents being moved for $($userhome) -ForegroundColor Yellow

}

else

{

Write-Host Documents already moved for $($userhome)

}

}

}

else

{

# Create the Documents folder

Get-Item -path $movesource | New-Item -ItemType Directory -Path "$movesource\Documents"

Write-Host Documents folder being created for $($userhome) -ForegroundColor Green

 

# Check that Documents folder now exists

If (Test-Path $movedest)

{

Write-Host Documents folder now exists for $($userhome) -ForegroundColor Magenta

Get-Item -path $movesource | New-Item -ItemType Directory -Path "$movesource\Documents\Autorecovery" | %{$_.Attributes="hidden"}

 

# Test if Autorecovery folder now exists

If (Test-Path $autodest)

{

Write-Host Autorecovery folder Created for $($userhome) -ForegroundColor Green

 

# Move Documents to new location

If (Test-Path $testpath)

{

Get-Childitem -path $movesource -exclude $exclude | Move-Item -Dest $movedest

Get-ChildItem -Path $movesource -Exclude "Documents" | ? {$_.PSIsContainer} |Remove-Item -Recurse -force

Write-Host Documents being moved for $($userhome) -ForegroundColor Yellow

}

else

{

Write-Host Documents already moved for $($userhome)

}

}

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 $($userhome) -ForegroundColor Green

 

# Check if Autorecovery folder exists

If (Test-Path $autodest)

{

Write-Host Autorecovery folder now exists for $($userhome) -ForegroundColor Magenta

 

# Move Documents to new location

If (Test-Path $testpath)

{

Get-Childitem -path $movesource -exclude $exclude | Move-Item -Dest $movedest

Get-ChildItem -Path $movesource -Exclude "Documents" | ? {$_.PSIsContainer} |Remove-Item -Recurse -force

Write-Host Documents being moved for $($userhome) -ForegroundColor Yellow

}

else

{

Write-Host Documents already moved for $($userhome)

}

}

else

{

Write-Host ERROR Autorecovery folder still does not exist for $($userhome) -ForegroundColor Red

}

}

}

else

{

Write-Host ERROR Documents folder still does not exist for $($userhome) -ForegroundColor Red

}

}

}

# Set user new home folder path

Get-ADUser -Filter * -SearchBase "OU=Users,DC=domain,DC=co,DC=uk" | Foreach-Object{

$sam = $_.SamAccountName

Set-ADuser -Identity $_ -HomeDrive "H:" -HomeDirectory "\\server\homefolders$\$sam\Documents"

Write-Host Home folder set for ($_.SamAccountName) -ForegroundColor Yellow

}

Edited by sthildas

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...