Skip to content

Instantly share code, notes, and snippets.

@vannmangel
Last active February 3, 2016 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vannmangel/0d1d1253b27d6dbda18a to your computer and use it in GitHub Desktop.
Save vannmangel/0d1d1253b27d6dbda18a to your computer and use it in GitHub Desktop.
Disable and move Active Directory computer objects to a specified OU.
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.99
Created on: 03.02.2016 13.58
Created by: Stian Myhre
Organization: Amedia Teknologi AS
Filename: DisableAndMoveComputerObjects.ps1
===========================================================================
.Synopsis
Disables and moves computer objects to a specified OU.
.EXAMPLE
DisableAndMoveComputerObjects -OS "Windows 7" -MinDaysOffline 30 -TargetPath "OU=Workstations,CN=Computers,DC=CONTOSO,DC=COM"
.EXAMPLE
DisableAndMoveComputerObjects -OS "Windows Server 2008" -MinDaysOffline 90 -TargetPath "OU=Workstations,CN=Computers,DC=CONTOSO,DC=COM" -WhatIf
#>
function DisableAndMoveComputerObjects
{
[CmdletBinding()]
[Alias()]
Param
(
[Parameter(ValueFromPipelineByPropertyName=$false,
Mandatory=$true,
HelpMessage="Filter OS. I.e. 'Windows Server', 'XP' or 'Windows 7'",
Position=0)]
[String]$OS,
[Parameter(ValueFromPipelineByPropertyName=$false,
Mandatory=$true,
HelpMessage="Define the minmum amount of days offline.",
Position=1)]
[int]$MinDaysOffline,
[Parameter(ValueFromPipelineByPropertyName=$false,
Mandatory=$true,
HelpMessage="Define the target OU distinguished Name. I.e.'OU=Workstations,CN=Computers,DC=CONTOSO,DC=COM'",
Position=2)]
[String]$TargetPath,
[Switch]$WhatIf
)
#declare wildcards for filtering
$OS = '*'+$OS+'*'
if ($WhatIf)
{
Get-ADComputer -Filter {OperatingSystem -like $OS} -Properties * | `
? {$_.LastLogonDate -le (Get-Date).AddDays(-$MinDaysOffline) -and $_.Enabled -eq $true} | `
% {Disable-ADAccount -Identity $_ -WhatIf; Move-ADObject -Identity $_.ObjectGUID.GUID -TargetPath $TargetPath -WhatIf}
}
else
{
Get-ADComputer -Filter {OperatingSystem -like $OS} -Properties * | `
? {$_.LastLogonDate -le (Get-Date).AddDays(-$MinDaysOffline) -and $_.Enabled -eq $true} | `
% {Disable-ADAccount -Identity $_ -Verbose ; Move-ADObject -Identity $_.ObjectGUID.GUID -TargetPath $TargetPath -Verbose}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment