PowerShell commands to remember
UPTIME
Command to display the days of uptime. Perfect for checking to see how long (in days) a computer has been running if you want to nag the user to reboot after windows updates have been installed.
Version 1:
powershell.exe -command (get-date)-([System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject win32_operatingsystem).lastbootuptime)) ^| select -ExpandProperty days
Version 2:
$os = Get-WmiObject win32_operatingsystem; $uptime = (Get-Date) – ($os.ConvertToDateTime($os.lastbootuptime)); Write-Host $Uptime.Days
Active Directory Last Password Change
get-aduser -filter * -properties passwordlastset, passwordneverexpires | ft Name, passwordlastset, Passwordneverexpires
spit it out as a report:
get-aduser -filter * -properties passwordlastset, passwordneverexpires | ft Name, passwordlastset, Passwordneverexpires | Out-File C:\path\lastpasswordlist.txt
Active Directory Last Password Change, if the account is enabled, and last logon date
get-aduser -filter * -properties passwordlastset, passwordneverexpires, enabled, lastlogondate | ft Name, passwordlastset, Passwordneverexpires, enabled, lastlogondate | Out-File C:\path\ADaccountStatus.txt
Active Directory Last time computer connected to domain
Get-ADComputer -Filter * -Properties * | Sort LastLogon | Select Name, LastLogonDate,@{Name=’LastLogon’;Expression={[DateTime]::FromFileTime($_.LastLogon)}}
and
Get-ADComputer –Filter * -Properties * | Sort LastLogon | Select Name, LastLogonDate,@{Name=’LastLogon’;Expression={[DateTime]::FromFileTime($_.LastLogon)}} | Export-Csv C:\adcomputers-last-logon.csv -NoTypeInformation
Run powershell commands on remotely another computer
Invoke-Command -ComputerName COMPUTERNAME -ScriptBlock {
msiexec /package c:\path\installer.msi /quiet /norestart
}
Invoke-Command -ComputerName COMPUTERNAME -ScriptBlock {
get-localuser
}