How I Use PowerShell to Simplify My Workflow on Windows 11

October 9, 2024

I can say I’m a big fan of the command line, even though I really enjoy minimalist and user-friendly interfaces. I’m quite familiar with using Unix-based and Linux operating systems like macOS, Ubuntu, and Debian, and I often use the command line to get the information I need.

If you're a developer, using the command line is essential. I don’t use Windows much, but I recently had to switch back after my workplace provided me with a Thinkpad running Windows 11. Since I hadn’t used Windows for a while, getting acquainted with its command line helped minimize the time spent learning how to extract information from the system.

Usually, when I encounter long command lines, I use Set-Alias to create a shortcut, which speeds things up. For example, to reboot the system, you would normally call Restart-Computer from PowerShell. Here’s how I assign it to a command similar to what you’d do on Ubuntu/Debian:

Set-Alias -Name reboot  -Value Restart-Computer

Now, when I need to reboot, I just type reboot :)

Here are some of my favorite command lines. Note that you’ll need to open PowerShell with Administrator rights, or you might face access denial errors.

Last boot up time

Get-CimInstance -ClassName win32_operatingsystem | select  lastbootuptime

or

(get-date) - (gcim Win32_OperatingSystem).LastBootUpTime

You’ll get results like this:

Days              : 1
Hours             : 0
Minutes           : 40
Seconds           : 47
Milliseconds      : 825
Ticks             : 888478255841
TotalDays         : 1.02833131463079
TotalHours        : 24.6799515511389
TotalMinutes      : 1480.79709306833
TotalSeconds      : 88847.8255841
TotalMilliseconds : 88847825.5841

Get-History

To view the history of the commands you’ve used:

Get-History

Out-file

To save the output to a raw text file:

Get-Service | Out-File C:\Services.txt

Out-Gridview

To interact with the output and display it in a grid view within a GUI window:

Get-Service | Out-Gridview

Get CPU architecture info

If you’re curious about your CPU type and architecture, use this command:

Get-WmiObject -Class Win32_ComputerSystem -ComputerName. | Select-Object -Property SystemType

or

(Get-CimInstance -ClassName Win32_OperatingSystem).OSArchitecture

Result:

SystemType
----------
x64-based PC

hostname

To get the name of your computer

hostname

Find your public IP

(Invoke-RestMethod ipinfo.io/json).ip

Get OS name

(Get-WmiObject win32_operatingsystem).name.split("|")[0]

Result:

Microsoft Windows 11 Business

Get Windows OS install date

([WMI] '').ConvertToDateTime((Get-WMIObject Win32_OperatingSystem).InstallDate)

Result:

Saturday, August 26, 2023 12:16:17 AM

Generate random password

[System.Web.Security.Membership]::GeneratePassword(12, 4)

Result:

(Uz*>y83cTs;

Additionally, one of my favorite commands on macOS/Linux is htop, unfortunately, it’s not available on Windows. Luckily, you can try btop4win, which lets you monitor hardware activities and system services, giving you an overview of what's going on under the hood.

btop4win