Jump to content

[Powershell] Move User Documents folder based on OU contents


Recommended Posts

Posted

The title is awful but I'm not sure how else to phrase it.

 

The problem I have is this:

 

We are looking at ways of automating the rollup of Students using Powershell. We are renaming OU's and importing the new starters into a new OU (YearR). Year1 becomes Year2 etc... This is easy enough and is already done. What we would like to do is move the documents folders for the Leavers into an archive folder. Ideally this should be one Powershell command. I don't have that firm a grasp of Powershell so I cannot work out how to do it. I understand how to read the data from the OU and get it to display he users Name. What I need to do is this:

 

Read Users Username from specified OU (what do I change in this command to do that - Get-ADObject -Filter 'Name -like "*"' -Searchbase 'OU=Year7,OU=Pupils,OU=Users,OU=School,DC=SchoolName,DC=Local')

Use the usernames listed in a Move-Item command

 

Hopefully this makes sense to someone.

Thanks

Posted
Pipe (|) to a Select-Object Name which will select only the Account name and then pipe that through to the move command. Or am I missing something?
Posted

This is pretty easy to achieve, I've got a similar script myself. The basics of it are below, our OUs are named after the intake year so it's pretty easy to do the calculation based on that to automatically work out which OU to deal with. For your naming system you could just use a parameter that you pass it and it does the work based on that instead.

 



$ClosingIntakeGroup = (Get-Date).Year -5


$Users = Get-ADUser -Filter * -SearchBase "OU=$ClosingIntakeGroup,OU=Students,OU=Users,DC=Domain,DC=local"


Foreach ($User in $Users) 
{


   Write-Output "Disabling account for $($User.SamAccountName)"
   Set-ADUser -Description "Disabled $(Get-Date)" -Identity $User.Samaccountname -Enabled $false -whatif
}


New-Item -Path "\\Server\StudentWork`$\Leavers\$((Get-Date).Year)" -ItemType Directory -Force -WhatIf


Move-Item -Path "\\Server\StudentWork`$\$ClosingIntakeGroup\*" -Destination "\\server\StudentWork`$\Leavers\$((Get-Date).Year)" -WhatIf

Posted
This is pretty easy to achieve, I've got a similar script myself. The basics of it are below, our OUs are named after the intake year so it's pretty easy to do the calculation based on that to automatically work out which OU to deal with. For your naming system you could just use a parameter that you pass it and it does the work based on that instead.

 



$ClosingIntakeGroup = (Get-Date).Year -5


$Users = Get-ADUser -Filter * -SearchBase "OU=$ClosingIntakeGroup,OU=Students,OU=Users,DC=Domain,DC=local"


Foreach ($User in $Users) 
{


   Write-Output "Disabling account for $($User.SamAccountName)"
   Set-ADUser -Description "Disabled $(Get-Date)" -Identity $User.Samaccountname -Enabled $false -whatif
}


New-Item -Path "\\Server\StudentWork`$\Leavers\$((Get-Date).Year)" -ItemType Directory -Force -WhatIf


Move-Item -Path "\\Server\StudentWork`$\$ClosingIntakeGroup\*" -Destination "\\server\StudentWork`$\Leavers\$((Get-Date).Year)" -WhatIf

 

Amazing, Thank you.

 

Going to go butcher this now :)

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...