{"id":814,"date":"2020-06-24T09:15:23","date_gmt":"2020-06-24T13:15:23","guid":{"rendered":"https:\/\/increasec.com\/?p=814"},"modified":"2026-03-09T12:45:46","modified_gmt":"2026-03-09T16:45:46","slug":"useful-powershell-snippets","status":"publish","type":"post","link":"https:\/\/increasec.com\/?p=814","title":{"rendered":"Useful PowerShell commands"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">List AD users that haven't logged on in 30 days<br><br><strong>Import-Module activedirectory<br>Get-ADUser -Filter {Enabled -eq $TRUE} -SearchBase $OU -Properties Name,SamAccountName,LastLogonDate | Where {($_.LastLogonDate -lt (Get-Date).AddDays(-30)) -and ($_.LastLogonDate -ne $NULL)} | Sort | Select Name,SamAccountName,LastLogonDate<br><\/strong><br>See if a tcp port is open\/blocked\/closed<br><strong>Test-NetConnection -ComputerName COMPUTER_NAME -Port PORT_NUMBER<br><\/strong><br><br><br>Set an interface to type public\/private\/domain<br><strong>Get-NetConnectionProfile<\/strong><br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-0-color\">Name : wtf.local<br>InterfaceAlias : Internal<br>InterfaceIndex : 12<br>NetworkCategory : DomainAuthenticated<br>IPv4Connectivity : LocalNetwork<br>IPv6Connectivity : LocalNetwork<br><br>Name : Network<br>InterfaceAlias : Internet<br>InterfaceIndex : 13<br>NetworkCategory : Public<br>IPv4Connectivity : LocalNetwork<br>IPv6Connectivity : LocalNetwork<br><\/mark><br>Set-NetConnectionProfile -InterfaceIndex 13 -NetworkCategory Private<br><br><br>see what DOMAIN user is logged on to a remote PC<br><strong>query user \/server:$SERVERNAME<\/strong><br><br>#powershell version<br>$PSVersionTable<br><br>#show what software needs upgrading<br>winget upgrade<br>#update all software<br>winget upgrade --all<br><br>#enable powershell remoting<br>Enable-PSRemoting -force<br><br>#show what users are logged onto this pc.  may work from cmd too<br>query user \/server:$SERVER<br><br>#Flush the DNS cache on a domain controller<br>Clear-DnsServerCache<br><br>#export GroupPolicy for support case or documentation<br>Import-Module GroupPolicy<br># Export a specific GPO<br>Get-GPOReport -Name \"Default Domain Policy\" -ReportType Html -Path Default.html<br>Get-GPOReport -Name \"Default Domain Policy\" -ReportType Xml -Path Default.xml<br># Export all GPOs<br>Get-GPOReport -All -ReportType Html -Path All.html<br>Get-GPOReport -All -ReportType Xml -Path All.xml<br><br>#get a cmd prompt on a remote pc, works in cmd too<br>WinRS -r:MYSERVER \"cmd.exe\"<br><br>#Install the DFS management console, some server versions don't install by default<br>Install-WindowsFeature RSAT-DFS-Mgmt-Con<br><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>List AD users that haven&#8217;t logged on in 30 daysImport-Module activedirectoryGet-ADUser -Filter {Enabled -eq $TRUE} -SearchBase $OU -Properties Name,SamAccountName,LastLogonDate | Where {($_.LastLogonDate -lt (Get-Date).AddDays(-30)) -and ($_.LastLogonDate -ne $NULL)} | Sort&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[122,121],"class_list":["post-814","post","type-post","status-publish","format-standard","hentry","category-uncategorised","tag-ad","tag-powershell"],"_links":{"self":[{"href":"https:\/\/increasec.com\/index.php?rest_route=\/wp\/v2\/posts\/814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/increasec.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/increasec.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/increasec.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/increasec.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=814"}],"version-history":[{"count":14,"href":"https:\/\/increasec.com\/index.php?rest_route=\/wp\/v2\/posts\/814\/revisions"}],"predecessor-version":[{"id":4147,"href":"https:\/\/increasec.com\/index.php?rest_route=\/wp\/v2\/posts\/814\/revisions\/4147"}],"wp:attachment":[{"href":"https:\/\/increasec.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/increasec.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/increasec.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}