Powershell

Bulk Delete of AD users from csv (Use with care)
Import-Csv E:\RemoveUser.csv | ForEach-Object {Remove-ADUser -Identity $_.SamAccountName -Confirm:$False

Format of csv file: 
Bulk Delete of DNS Records from csv (Use with care)
 Import-Csv E:\RemoveDns.csv | ForEach-Object {Remove-DnsServerResourceRecord -Name $_.name -RRType $_.RRType -ZoneName MyDomain.com -ComputerName MyServer -Confirm}

Format of csv file:


How to find LAPS password:
 Get-ADComputer $ADcomputer -Properties ms-Mcs-AdmPwd,ms-Mcs-AdmPwdExpirationTime

How to find DC information:
nltest /dsgetdc:mydomain.com

How to Find FC WWN
Get-WmiObject -class MSFC_FCAdapterHBAAttributes -namespace "root\WMI" | ForEach-Object {(($_.NodeWWN) | ForEach-Object {"{0:x}" -f $_}) -join ":"}

How to get BitLocker Key of computer in AD:
Get-ADComputer -Identity MyComputer | ForEach-Object -Process {Get-ADObject -SearchBase $_.DistinguishedName -Filter {objectClass -eq 'msFVE-RecoveryInformation'} -Properties 'msFVE-RecoveryPassword'}

How to get BitLocker Key from local computer:
(Get-BitLockerVolume -MountPoint C).KeyProtector

How to Find Server OS information in AD
Get-AdComputer -LDAPFilter "(OperatingSystem=*Server*)" -Properties OperatingSystem | Format-List Name, OperatingSystem* > E:\ServerList.txt

How to Start and Stop a service on a remote server:
Get-Service -Name "VMware vSphere Profile-Driven Storage Service" -ComputerName MyComputer | Start-service
Get-Service -Name "VMware vSphere Profile-Driven Storage Service" -ComputerName MyComputer | Stop-service


How to Find an AD User with their Surname:
get-aduser -filter {Surname -eq "umemoto"} | Select SamAccountName

How to find last bootup:
Get-CimInstance -ClassName win32_operatingsystem | select csname, lastbootuptime

How to get VM info from a Hyper-V host:
get-vm -computername $hypervhost | format-table -property vmname, notes -AutoSize -wrap

How to get the status of a service on a remote server:
Get-Service -Name "VMware vSphere Profile-Driven Storage Service" -ComputerName MyComputer

How to find out what version of Powershell you're running:
(If no response, assume Version 1)
$psversiontable

How to export to csv:
get-customattribute | export-csv e:\customattribute.csv

Gracefully shut down computer:
Stop-Computer -ComputerName camputername -Confirm -force

Removing computer for AD:
Remove-ADComputer -Identity computername

Adding a DNS Alias:
Add-DnsServerResourceRecordCName -Name "labsrv1" -HostNameAlias "srv1.lab.contoso.com." -ZoneName "contoso.com" -ComputerName "YourDNSServer"

Adding a DNS Host A record (Including PTR record): 
Add-DnsServerResourceRecordA -IPv4Address "xxx.xxx.xxx.xxx" -Name "servername" -ZoneName "contoso.com" -ComputerName "DNSServer" -CreatePtr

Removing a DNS Host A record:
Remove-DnsServerResourceRecord -zonename contoso.com -RRtype A -Name servername -computername dnsserver

Find last bootup time:
Get-CimInstance -ClassName win32_operatingsystem | select csname, lastbootuptime

Adding server to a domain and into a specific OU:
add-computer -domainname contoso.com -credential "contoso\MyAccount" -OUpath "OU=Servers,DC=contoso,DC=com"

Unlock AD account
Unlock-ADAccount -Identity UserName

Finding last logon of a computer:
# Gets host and lastLogonTimestamp in UTC of specified host
# get Name
$hostname = Read-host "Enter a hostname"
# grab the lastLogonTimestamp attribute
Get-ADComputer $hostname -Properties lastlogontimestamp |
# output hostname and timestamp in human readable format
Select-Object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp


Get Members of an AD group
Get-ADGroupMember -Identity GroupName

Getting computer info:
Get-ADComputer -Identity computername

Getting user info:
Get-ADUser - Identity username

Running remote signed scripts (Use with caution):
Set-ExecutionPolicy RemoteSigned

How to find open ports:
netstat -aon | more

Get VM network info:
get-vmnetworkadapter -vmname myvms*
 

No comments:

Post a Comment