Sunday, February 27, 2022

Powershell Tutorial: Useful CMD in Powershell

When I talk to people about Powershell, there seems to be a lot of hesitance to use it. It's always just easier for people I know to rattle off whatever obscure CMD command they know. The combination of exe programs that have no Powershell equivalent but maybe offer a few wrapper libraries or copy-paste functions can make it seem even less useful to learn how to do things in Powershell. The biggest point I can make for using Powershell is the ease of data processing. Throw that in with .Net and you actually have a very flexible toolset. If you want to advance to scripting and automation, it is in my opinion advantageous to learn whatever tools offer the best for gathering, processing, and even reporting data. Powershell fits this really well, and I am genuinely surprised at how easy it feels once you start getting into using it. To that end, here is a list of common commands I use a lot on command line and their CMD equivalent. Some are a bit more extensive just to match the output, so it may also be worth looking into making your own module or making some functions for the Powershell profile script.

  • Get the IP address
    • ipconfig
    • Get-NetIPAddress
  • Get the mac address
    • ipconfig /all
    • Get-NetAdapter
  • Get the DNS servers
    • ipconfig /all
    • Get-NetIPConfiguration
  • Trace route
    • tracert <destination>
    • Test-NetConnection -TraceRoute <destination>
  • Ping
    • ping <destination>
    • Test-Connection <destination>
  • Netstat is a little more complicated because UDP and TCP are different commands
    • netstat
    • Get-NetTCPConnection / Get-NetUDPEndpoint
  • Netstat but with process information attached (powershell uses pid, but you can fudge the names in there)
    • netstat -b
    • Get-NetTCPConnection | select localaddress,localport,remoteaddress,remoteport,state,@{N="Process";E={$(gps -pid $_.owningprocess).processname}}|ft
  • Update group policy for domain computers (Powershell can do so remotely)
    • gpupdate /force
    • Invoke-GPUpdate -Computer <computername> -Force
  • Rename a computer (Powershell works remotely here as well)
    • WMIC computersystem where caption='OldComputerName' rename NewComputerName
    • Rename-Computer -ComputerName <oldname> -NewName <newname> -Restart
This is hardly an extensive list. It is true that in most cases the CMD versions are shorter. It is also true that the CMD versions often times use very obscure abbreviated names, whereas Powershell is very verbose in naming. There is also a long list of very useful aliases that make a translation between CMD to Powershell and even Bash to Powershell, and some shortened common Powershell command. For example, list directory in Powershell Get-ChildItem is aliased to gci for short, dir for familiarity with cmd, and ls for familiarity with Bash. While it may seem inconvenient, Powershell is actually very good at dealing with large batch processes and very easily controlling data flow and even parallel processing (which may seem weird bit can be a huge time saver).

I hope this list helps some people get used to running some basic Powershell tasks, Throw in some basic loops and branching and you're already where you need to begin making very comprehensive automation scripts.

No comments:

Post a Comment

Tag Cloud

.NET (2) A+ (5) ad ds (1) addon (4) Android (4) anonymous functions (1) application (9) arduino (1) artificial intelligence (1) backup (1) bash (6) camera (2) certifications (3) comptia (5) css (2) customize (11) encryption (3) error (13) exploit (5) ftp (1) funny (4) gadget (4) games (3) GUI (5) hardware (16) haskell (6) help (14) HTML (3) imaging (2) irc (1) it (1) java (2) javascript (13) jobs (1) Linux (19) lua (1) Mac (4) malware (1) math (6) msp (1) network (13) perl (2) php (3) plugin (2) powershell (8) privacy (2) programming (24) python (10) radio (2) regex (3) repair (2) security (16) sound (2) speakers (2) ssh (1) story (5) Techs from the Crypt (5) telnet (1) tools (13) troubleshooting (11) tutorial (9) Ubuntu (4) Unix (2) virtualization (2) web design (6) Windows (16) world of warcraft (1) wow (1) wx (1)