Skip to content

Instantly share code, notes, and snippets.

@ScriptingPro
ScriptingPro / find dupe files by hash.ps1
Created October 25, 2017 23:57
powershell find duplicate files
# powershell find duplicate files md5
# powershell get childitem filter duplicates
# script to find duplicate files windows
# powershell duplicate files md5
gci * -Recurse | get-filehash -Algorithm MD5 | Group-Object hash | ?{$_.count -gt 1} | select @{n='DupeCount';e={$_.Count}}, @{n='DupeFiles';e={$_.Group.Path -join [System.Environment]::NewLine}} | Out-GridView
@ScriptingPro
ScriptingPro / System.DirectoryServices.ActiveDirectory.ps1
Last active February 16, 2024 00:42
Query Active Directory without ActiveDirectory Module
#get dnsroot
[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().Name
#get domain distinguishedname
[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().GetDirectoryEntry().Properties["distinguishedName"]
#list domain controllers
[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().FindAllDomainControllers().Name
#get PDC
@ScriptingPro
ScriptingPro / Active_Directory_Port_Check.ps1
Created November 16, 2022 22:35
Check if necessary AD Ports are Open using PowerShell
$TargetDC = “dc1.contoso.com”
Test-NetConnection -ComputerName $TargetDC -Port 88 # Kerberos
Test-NetConnection -ComputerName $TargetDC -Port 135 # RPC
Test-NetConnection -ComputerName $TargetDC -Port 139 # NetBIOS SS
Test-NetConnection -ComputerName $TargetDC -Port 389 # LDAP
@ScriptingPro
ScriptingPro / Validate AD SRV DNS Records.ps1
Last active February 16, 2024 00:41
Check Active Directory DNS SRV Records
# validate srv records
$domain = 'contoso.com'
$sites = 'Dallas','Austin','Houston'
foreach($site in $sites){
@"
_kerberos._udp.$domain
_kpasswd._udp.$domain
_gc._tcp.$domain
@ScriptingPro
ScriptingPro / LockoutFinder.ps1
Last active February 16, 2024 00:41
Find the source of AD user's account lockouts
Import-Module ActiveDirectory
$samID = "USERIDHERE"
$Host.UI.RawUI.WindowTitle = "Finding lockouts for $samID" #change window title just incase we have multiple running
$DCs = Get-ADDomainController -Filter * | select -ExpandProperty name
# do infinite loop, sleeping for 60 seconds each iteration, and when i find the account locked search for lockout source and log it
do{
@ScriptingPro
ScriptingPro / Get-Local-Group-Members.ps1
Created May 25, 2022 16:18
get local group members powershell remote computer
PS C:\Windows\system32> $htVmDatacenters.Values | %{
Get-WmiObject -ComputerName $_.Replace("`$", '') -Query "SELECT * FROM Win32_GroupUser" | ?{([WMI]$_.GroupComponent).Caption -like "*\Administrators"} | %{
$PartComponent = $_.PartComponent -replace "^.*\\cimv2:","Class=" -replace '"','' -replace "[\.,]",[environment]::NewLine | ConvertFrom-StringData
[PSCustomObject]@{
LocalGroup = ([WMI]$_.GroupComponent).Caption
Member = "$($PartComponent.Domain)\$($PartComponent.Name)"
}
}
}
# directoryentry (connect to specific dc:port)
$de = ([ADSI]"LDAP://$($ADDomainController.HostName):$($ADDomainController.LdapPort)")
# directorysearcher
$ds = New-Object System.DirectoryServices.DirectorySearcher($de,"(objectclass=user)")
$ds.PageSize=1000
# invoke
$ds.FindAll() | %{"do stuff with $_"}
$thisuser = $ds.FindOne()
@ScriptingPro
ScriptingPro / LDAP-Request.ps1
Created November 22, 2017 18:06
LdapConnection AddRequest SendRequest
$AuthenticationType = [System.DirectoryServices.AuthenticationTypes]::Secure
$DirectoryConnection = New-Object -TypeName System.DirectoryServices.Protocols.LdapConnection -ArgumentList "ldapserver.domain.com:777,uid=newuser,ou=people,dc=company,dc=com", $password , $AuthenticationType
$DirectoryConnection.Bind()
$DirectoryRequest = New-Object -TypeName System.DirectoryServices.Protocols.AddRequest
$DirectoryRequest.DistinguishedName = "uid= xxxx, ou=user, o=company"
$DirectoryRequest.Attributes.Add((New-Object -TypeName System.DirectoryServices.Protocols.DirectoryAttribute -ArgumentList "objectclass",@("top","organizationalPerson","person","inetorgperson","inetuser","mailrecipient","pwmuser","posixAccount"))) | Out-Null
$DirectoryRequest.Attributes.Add((New-Object -TypeName System.DirectoryServices.Protocols.DirectoryAttribute -ArgumentList "cn",($FirstName+" "+$LastName))) | Out-Null
$DirectoryConnection.SendRequest($DirectoryRequest)
@ScriptingPro
ScriptingPro / PowerShell LDAP.ps1
Created January 4, 2018 22:33
LDAP bind to server/port with PowerShell using DirectoryEntry Class and query with DirectorySearcher Class
# define your query filter
$LDAPfilter = "(samaccountname=*)"
# define the object attributes you want returned
$Attributes = @"
samaccountname
givenname
surname
mail
@ScriptingPro
ScriptingPro / check-kms-ports.ps1
Created August 1, 2023 19:00
Check KMS Activation Server ports
# finds kms servers from DNS and checks if the port is open
Resolve-DnsName "_vlmcs._tcp.$([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Name)" -Type all | %{Test-NetConnection -ComputerName $_.NameTarget -Port 1688}