Cool things about powershell

Typing powershell at the cmd.exe prompt C:Usersandrew starts Windows PowerShell

A look at the Windows PowerShell commands that make the shell feel familiar to a long-time UNIX/Linux admin: getting system information, clearing the screen, reading help, network settings, editing files, memory, disk space and restarting services. It was written in 2012 for Windows PowerShell 2.0 on Windows 7 and Windows Server and is republished from the original blog with its screenshots; notes for current Windows Server and PowerShell releases are at the end.

PowerShell has come a long way since version 1.0. PowerShell is no longer “Powerhell” (although it still needs some work).

Windows PowerShell window (Copyright 2009 Microsoft Corporation) at the PS Z:\> prompt

but it’s enough to mention that as a long time UNIX/Linux shell user, Microsoft has made enough progress for me to consider adopting PowerShell for a few reasons:

  1. Server 2012: there’s no other way
  2. Removing the graphical overhead of Explorer and applications has its benefits
  3. Windows updates only need to be applied 1-2 times PER YEAR if you configure the server right (pretty awesome, but not Linux)
  4. PowerShell supports a lot of common UNIX/Linux commands, so it’s not that hard of a transition.

What still needs work

Let’s talk about some reasons that PowerShell still needs some work:

1) Each role/server you have to learn a separate set of cmdlets, so you can be really advanced with normal PowerShell, until you need to perform an Exchange PowerShell command, and you see the famous “what are you talking about” error

PowerShell error: the term is not recognized as the name of a cmdlet, function, script file, or operable program

2) PowerShell does not allow remote shell, so like in traditional UNIX/Linux systems, I can’t “POWERSHELL” into another box. Microsoft wants you to enable remote desktop (kind of doesn’t make sense). You can do this with Telnet, but that would eliminate the idea of the extra security. (2026 note: this has changed; see the notes at the end.)

Start PowerShell from cmd

Anyways, now that I’m off my soapbox, I think it’s time to take a look at it. Microsoft has done some cool things to make PowerShell easy, for example, you can go from cmd to PowerShell easily by typing:

powershell
Typing powershell at the cmd.exe prompt C:\Users\andrew starts Windows PowerShell

Uptime: systeminfo

What are the main things you do from a UNIX/Linux command prompt for administration? Uptime?

PowerShell can do that with “systeminfo” (look for System Boot Time):

systeminfo
systeminfo output in PowerShell with the System Boot Time line underlined

clear and man

ok, now that our screen is all mucked up, let’s use a UNIX command:

clear
Empty PowerShell window at PS Z:\> after running clear

that’s pretty cool, didn’t expect that?

what about if you don’t know what you are doing? Guess what, RTFM: type the command “man”, oh yeah, they worked that in there too

man
man in PowerShell displays the Get-Help topic

ipconfig /all

what about the old ipconfig /all?

ipconfig /all
ipconfig /all in PowerShell showing the IPv4 address, default gateway, DNS servers and WINS servers

that beats ifconfig because it gives the IP and the DNS… (cracking a smile yet?)

Edit a file with notepad

Alright, fine, what about editing code, how do you do that in Windows PowerShell?

they have an answer for that too! say we have a file in our C:/ named test, we can from PowerShell edit the file with notepad

notepad.exe C:/test
notepad.exe C:/test run from PowerShell opens the file test in Notepad

Free memory

Since when did Microsoft get so good at the shell? Alright, well for sure they don’t have a way to check free memory

get-wmiobject win32_computersystem
get-wmiobject win32_computersystem output including TotalPhysicalMemory

Again, where did Microsoft learn this?

Disk space

ok, ok, I got one, what about disk space?

Get-WMIObject Win32_LogicalDisk
Get-WMIObject Win32_LogicalDisk listing DeviceID, DriveType, FreeSpace and Size for each drive

not only does it show that for local volumes, it also shows shares and removable media.

Restart a service

ok, what about restarting services? they have that too

Restart-Service Netman -f

restarts network connections; the -f is to force because it has dependencies

Restart-Service netman -f entered in PowerShell

Most file move commands are the same such as cp, mv, cd, ls.

…It’s time to blow the lid off this: PowerShell is here and it’s here to stay, and for the smart admins that want to make their job easier by eliminating the GUI, this is the way to go.

Notes for current Windows Server and PowerShell

  • Remote shells exist now. PowerShell 2.0 already had remoting over WinRM (Enter-PSSession -ComputerName server1 for an interactive session, Invoke-Command to run commands on many servers), and it is enabled by default on Windows Server 2012 and later. See Enable Remote PowerShell Access. Current Windows Server releases also include an OpenSSH server feature, so ssh into a Windows box works much like on Linux.
  • cp, mv, ls, cd, clear and man are aliases for Copy-Item, Move-Item, Get-ChildItem, Set-Location, Clear-Host and help/Get-Help. They accept PowerShell parameters, not GNU options, so ls -la does not work the way it does on Linux.
  • The *-WmiObject cmdlets are not available in PowerShell 7; use Get-CimInstance instead: Get-CimInstance Win32_ComputerSystem for memory, Get-CimInstance Win32_LogicalDisk or Get-Volume for disk space.
  • Uptime directly: (Get-CimInstance Win32_OperatingSystem).LastBootUpTime, or Get-Uptime in PowerShell 7.
  • Network settings: Get-NetIPConfiguration is the PowerShell equivalent of ipconfig /all.
  • Windows still needs regular monthly updates; plan patching as a normal monthly task rather than once or twice a year.
  • If you manage Linux too, PowerShell 7 runs on AlmaLinux, Rocky Linux and RHEL (Microsoft publishes RPM packages), so the same scripts can run on both.

That’s it!

Related guides: Enable Remote PowerShell Access | Get Windows Server Statistics | Reboot Windows Server Core

Leave a Reply

Your email address will not be published. Required fields are marked *