Jump to content

Recommended Posts

Posted

Hi all,

 

I have this script which I got from here, would anyone be able to help me to modify this so that it only archives users under a certain OU e.g. Students ?:

 

ForEach ($HomePath in (Get-ADUser -Filter {Enabled -eq $false} -Property HomeDirectory).HomeDirectory) {

Move-Item -Path $HomePath -Destination "ARCHIVE AREA"

 

Thanks.

Posted

Should just be able to add a searchbase in shouldn't you?

 

e.g.

 Get-ADUser -SearchBase "OU=Students,DC=MyEpicDomain" -Filter {Enabled -eq $false} -Property HomeDirectory).HomeDirectory

 

Would need to test it obviously :)

 

-Edit - Aye seems you can https://technet.microsoft.com/en-us/library/ee617241.aspx?f=255&MSPPError=-2147217396

 

Example 1 - Get-ADUser -Filter * -SearchBase "OU=Finance,OU=UserAccounts,DC=FABRIKAM,DC=COM"

 

Steve

  • 4 weeks later...
Posted

Does this script look correct to anyone?

 

Get-ADUser -SearchBase "OU=Leavers,OU=Students,OU=Central,DC=DOMAIN.local" -Filter {Enabled -eq $false} -Property HomeDirectory).HomeDirectory{Move-Item -Path $HomePath -Destination "NETWORK ARCHIVE PATH"

 

Thanks

Posted
Does this script look correct to anyone?

Not quite. Try this...

 

$Leavers = Get-ADUser -Filter { Enabled -eq $false } -SearchBase "[color="#4B0082"]OU=Students,DC=example,DC=net[/color]" -Properties HomeDirectory

ForEach ($User in $Leavers) {

   $ArchivePath = [io.path]::Combine("[color="#4B0082"]X:\Users\Archived[/color]", $User.SamAccountName)

   If (Test-Path $User.HomeDirectory) {
       Move-Item –Path $User.HomeDirectory -Destination $ArchivePath -Force
   }

}

 

If you want to test the script without moving any folders, add -WhatIf to the end of Move-Item.

 

Use the following script if you want to move only the home directories of users who haven't logged in for 90 days.

 

$DaysInactive = (Get-Date).AddDays(-90)
$Leavers = Get-ADUser -Filter { Enabled -eq $false -and LastLogonTimeStamp -ge $DaysInactive } -SearchBase "[color="#4B0082"]OU=Students,DC=example,DC=net[/color]" -Properties HomeDirectory

ForEach ($User in $Leavers) {

   $ArchivePath = [io.path]::Combine("[color="#4B0082"]X:\Users\Archived[/color]",$User.SamAccountName)

   If (Test-Path $User.HomeDirectory) {
       Move-Item –Path $User.HomeDirectory -Destination $ArchivePath -Force
   }

}

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