Powershell useful commands

1. $PSVErsion - info about PS version

2. Start-Transcript, Stop-Transcript -  writes all output to .\Documents\Powershell folder

3. Pressing Tab  to search possible parameters of command such 

as Get-Process - and pressing Tab shows you all possible variants 

and Shift-Tab return back to previous parameter

4. Ctrl+Space show you all command possibilities like 

Get-Process -Name C+CTRL+SPACE shows you all processes with name starting with letter "c":

 Get-Process -Name cyber-protect-service
chrome                 conhost                csrss                  cyber-protect-service

and you can select it by pressing left and rights arrow key 

5. CTRL+R to search all previous entered commands

6.  Get-PSReadlineKeyHandler shows you all key key commands 

7. Help, help command more useful then get-help

get-help - showindow opens in new window with search

-Examples, -Online, -Detail

8. In commands help yo can view syntax and you can use only  and only one for commands

[-Name]   square brackets means that is positional parameters

string[] - shows that accept many parameters

Name
    Get-Service

SYNTAX
    Get-Service [[-Name] <string[]>]  [<CommonParameters>]

    Get-Service  [<CommonParameters>]

    Get-Service  [<CommonParameters>]


9. Finding commands

help *user*

 

10. Powershell modules

get-module shows loaded module  

Get-Module -ListAvailable 

Find-module Az

Install-Module Az

 

11. General Server Management

Get-ComputerInfo

Get-WindowsFeature show all windows features with right system names

Add-,Restart-,Rename-Computer

Restart-Computer -ComputerName SRV -Wait -For PowerShell : Write-Host "DONE"

Test-ComputerSecureChannel -test and Fix AD join problem

 

12. Networking settings

 Get-NetIPAddress

Get-NetAdapterStatistics 

Get-DNSClient

Get-DnsClientServerAddress

 Get-NetFirewallRule

Test-NetConnection www.mail.ru

Test-NetConnection www.mail.ru -Port 80

Test-NetConnection www.mail.ru -TraceRoute - nice to trace path 

Resolve-DnsName mail.ru like dig in Linux


13. Compare operators

equal

PS C:\Users\Administrator> 5 -eq 4

False

greater

PS C:\Users\Administrator> 5 -gt 4

True

Like

PS C:\Users\Administrator> "TEST" -like "something"

False

PS C:\Users\Administrator> "TEST" -like "TE"

False

PS C:\Users\Administrator> "TEST" -like "Ttest"

False

PS C:\Users\Administrator> "TEST" -like "Test"

True

Case sensitive like

PS C:\Users\Administrator> "TEST" -clike "Test"

False

PS C:\Users\Administrator> "TEST" -clike "TE"

False

PS C:\Users\Administrator> "TEST" -clike "TEST"


14. Search-ADAccount -AccountDisabled 

15. Customizing AD Searches

Shows all users:

Get-ADUser -Filter *

Search IN certain OU:

Get-ADUser -Filter * -SearchBase "OU=Users-LAB,DC=lab,DC=local"

Get all users lastlogon dates by names and departments and write them to table in needed format:

Get-ADUser -Filter * -SearchBase "OU=Users-LAB,DC=lab,DC=local" -Properties Lastlogondate,Department | Format-Table name,lastlogondate,department

name     lastlogondate       department

----     -------------       ----------

JDoe    14.04.2022 12:01:15

or you can add * to end of Properties to get  all props from all users

 Get-ADUser -Filter * -SearchBase "OU=Users-LAB,DC=lab,DC=local" -Properties *

Get-ADUser John Doe

get users from with some departments or other filled info:

Get-ADUser -Filter {department -eq 'Accounting'}

from to or more departments or AD attributes:

 Get-ADUser -Filter {department -eq 'Accounting' -or department -eq 'Sales'}

get all departments contains i letter

Get-ADUser -Filter {department -like '*a*'}


22. Passing PipelineData By Value

cat .\computers.txt | tnc

ComputerName           : www.mail.ru

RemoteAddress          : 94.100.180.70

InterfaceAlias         : Ethernet

SourceAddress          : 212.24.57.47

PingSucceeded          : True

PingReplyDetails (RTT) : 1 ms


ComputerName           : www.yandex.ru

RemoteAddress          : 5.255.255.70

InterfaceAlias         : Ethernet

SourceAddress          : 212.24.57.47

PingSucceeded          : True

PingReplyDetails (RTT) : 4 ms

Get-Content .\computers.txt | Test-NetConnection

'rds01','www.mail.ru' | Test-NetConnection


22. Passing Pipeline Data By PropertyName


import-csv .\computers.csv | Test-NetConnection


24. Using parenthesis (круглые скобки) to change the order of operations

Parenthesis - in PS using to change the order of executions

We can get computernames from a text file to input it in commands in one liner but we should to get it first so we can use () parenthesis to to get compnames and insert it to comands

Test-Connection -ComputerName (Get-Content .\computers.txt)

1. PS gets all names from txt

2. Second insert all of them to pipeline 

3. But without () command wont successful


 

Комментарии

Популярные сообщения из этого блога

VMware Horizon View не работают USB токены Rutoken Lite подключенные через нулевой клиент на базе протокола PCoIP Terradici