Use Powershell to Disable Computer and User accounts in Active Directory over XXX days

As a consultant for clients one common problem I come across is IT doing a poor job at managing old objects in Active Directory.  So generally I have to do that cleanup.  Here are some simple Powershell commands that I find help disable these objects for security reasons.  Once you have disabled them and let changes soak for a bit you can easily find the disabled accounts and delete them.

 

OpenPowershell as an Administrator

 

Import the ActiveDirectory Module for PowerShell.
Import-Module activedirectory

 

Set the number of days you want to check for inactivity, in my examples I will use 120 days.
$datecutoff = (Get-Date).AddDays(-120)

 

To Simply List those that have not been logged into in last 120 days (or # of days defined above)

Computers:

Get-ADComputer  -Properties LastLogonDate -Filter {LastLogonDate -lt $datecutoff} | Sort LastLogonDate | FT Name, LastLogonDate –Autosize

Users:

Get-ADUser  -Properties LastLogonDate -Filter {LastLogonDate -lt $datecutoff} | Sort LastLogonDate | FT Name, LastLogonDate –Autosize

 

To test the process but not execute the actual disable using the above criteria.

Computers:

Get-ADComputer  -Properties LastLogonDate -Filter {LastLogonDate -lt $datecutoff} | Set-ADComputer -Enabled $false –whatif

Users:

Get-ADUser  -Properties LastLogonDate -Filter {LastLogonDate -lt $datecutoff} | Set-ADUser -Enabled $false –whatif

 

Preform/Execute the process and execute the actual disable using the above criteria.

Computers:

Get-ADComputer  -Properties LastLogonDate -Filter {LastLogonDate -lt $datecutoff} | Set-ADComputer -Enabled $false

Users:

Get-ADUser  -Properties LastLogonDate -Filter {LastLogonDate -lt $datecutoff} | Set-ADUser -Enabled $false


Hope anyone who finds this article finds it as useful as the commands have been for me.

 

Powershell IP Changes Static/DHCP

Recently, a Windows 10 computer I was working on would not allow me to change any network setting from the GUI.  I needed to change to a static IP address from DHCP to do some maintenance.  I also needed to add a secondary IP address.  How did I do this?  The step are below and may come in handy to someone else.

Set IP Address to Static from Powershell

  1. Disable DHCP
    Open PowerShell
    Get-NetAdapter


    (note) review the output from the above command to get the Interface Name
    Get-NetAdapter –Name Ethernet
    Set-NetIPInterface -DHCP Disable

  2. Set IP Address

    New-NetIPAddress -AddressFamily IPv4 –InterfaceAlias “Ethernet” -IPAddress 192.168.1.100 -PrefixLength 24 -Type Unicast -DefaultGateway 192.168.1.1

    (note) -IPAddress 192.168.1.100 change to the IP Address you want to assign

    (note) –PrefixLength is the Mask -24 is 255.255.255.0

    (note) –DefaultGateway 192.168.1.1 change to the gateway for your network
    Set Static DNS Servers

    Primary:               netsh interface ip add dns name="Ethernet" addr=192.168.1.10 index=1
    Secondary:         netsh interface ip add dns name="Ethernet" addr=192.168.1.11 index=2
    Tertiary:               netsh interface ip add dns name="Ethernet" addr=8.8.8.8 index=3

  3. Add Secondary IP Address (If you want to add another IP Address to your interface)
    netsh interface ipv4 add address name=Ethernet 192.168.2.100 mask=255.255.255.0

Set DHCP from Powershell

  1. Enable DHCP

    Get-NetAdapter -Name Ethernet
    Set-NetIPInterface -DHCP Enable
    netsh interface ip set dns name="Ethernet" dhcp