
10 Cool and Useful PowerShell Commands for Enhanced Productivity
If PowerShell’s learning curve puts you off, then you probably don’t associate the word “cool” with it. But PowerShell is here to stay. It is a core part of Microsoft 365, Azure, and Windows Server 2022 and has powerful capabilities.
In this article, TechRepublic offers some tips that can come in handy. Plus, it’s always cooler when you surprise someone with a solution you provide via the command line. Having someone watch you right-click and fix something doesn’t have the same appeal.
Note: be careful, be very careful
Yes, this is a tool worthy of its name. PowerShell can easily cause large-scale configuration changes, both positive and negative – so protect yourself and set up a test environment for your learning experience. Also, consider using -confirm
Parameters to test the configuration before executing certain commands.
1. Report all installed USB devices
PowerShell supports Windows Management Instrumentation. From PowerShell, you can make a WMI call to retrieve a USB device installed on the local or remote system:
gwmi Win32_USBControllerDevice -computername SERVER1 |fl Antecedent,Dependent
This filter will bring back the antecedent and dependent fields from the SERVER1 computer. If you want a complete export, you can omit the pipe, | , filter statements to fully export the USB devices on your system.
This is useful for maintaining reports on servers with USB license devices installed, to maintain connectivity from the device’s perspective.
2. Perform your favorite command prompt character tasks
All tasks performed in the command prompt can also be completed in PowerShell. This can help you become more familiar with the interface.
Use the following command to launch PowerShell in the Execute dialog box powershell
. You can also assign a key shortcut to PowerShell so that Ctrl + Shift + P launches it directly.
3. Kill the process in PowerShell instead of Task Manager
When you are executing a Windows service that does not respond to a stop command, you can use PowerShell to perform the equivalent of ending the task in the Work Administrator. For example, you can do the following with BadThread.exe:
get-process BadTh*
The result will be similar to this:
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
28 4 -210844 -201128 -163 25.67 2792 BadThread
After identifying the process ID, you can terminate the errant process by entering the following command:
stop-process -id 2792
At this point, the BadThread example will be hard stopped and you can continue trying to start the service. You can also do this in PowerShell.
However, if the process does not terminate gracefully, you can use the -Force parameter:
stop-process -id 2792 -Force
Be careful when using it as data may be lost or corrupted if the process is performing a task.
4. Use PSDrive not only to view the drive
The PSDrive command allows you to view Windows environment objects outside of traditional network, local, or removable drives. One popular view is HKLM PSDrive, which allows you to view the HKEY_LOCAL_MACHINE top-level hive of the registry. To enter the registry, enter the following command:
PS C:> cd HKLM:
PS HKLM:/>
You will then be taken to the registry hive and can view or even delete items if you wish.
5. Export NTFS folder permissions – recursive or not
Managing NTFS permissions is another story, but using PowerShell, you can export permissions to audit access or quickly view access control lists for security configurations. This can be a great accountability mechanism and can be run in script format on a regular basis, or you can run it on demand to diagnose specific issues.
For example, take the following iteration:
PS E:>Get-Acl N:Data
This will give you a quick report on the security permissions for the specified path (note that it will not provide shared access). There’s nothing too exciting about this alone, as it will only report on a single specified path. But if you want to include recursion throughout the path, you can use other strategies.
For the same N:\Data path you can use Get-ChildItem
Commands in PowerShell, combined with Get-Acl
Order. Consider the following example:
PS E:>Get-ChildItem N:Data -recurse | Get-Acl
This will span the entire N:\Data path and display the ACL for the contents of that path. What’s happening here is Get-ChildItem
Provides a list of file system objects and passes the collection to Get-Acl
Provide results for each project.
If you want to archive it into a comma separated variable (CSV) file, you can pass | export-csv c:\filename.csv
at the end of the command. It can also pass through normally > C:\filename.txt
Go to the end of the command to export it to a text file.
Please note that when you use -recurse
option, it does just that and will traverse the entire path you specify. Therefore, be careful when operating in large volumes or over a network.
6. Put time-consuming tasks in the background
If your command or cmdlet takes some time to run, you can use PowerShell to send it to the background to complete. This way you can send a series of commands to be executed immediately and have them complete as scheduled.
The command to start a background job starts with start-psjob
scope. You can query the status of any job in PowerShell using the following command:
PS C:>get-psjob
You will see a results table showing the status of the jobs, where each job has a unique session identifier. You can delete any failed jobs by executing the following command:
PS C:>remove-psjob 9
7. Insert timestamp into PowerShell output
For PowerShell tasks, you can continuously enter timestamps to determine when a single step occurs or use it as a logging mechanism for scripts.
To insert a timestamp, enter one of the following commands as a single line in the .ps1 file:
date format | Order | Output example |
---|---|---|
Generally short (g ) |
$(Get-Date -format g) Start logging |
12/12/2024 9:15 PM |
Full date/time (F ) |
$(Get-Date -format F) Start logging |
Thursday, December 12, 2024 9:15:13 PM |
round trip(o ) |
$(Get-Date -format o) Start logging |
2024-12-12T21:15:13.0368750-05:00 |
There are many other formats Get-Date
command, but these three options are generally suitable for most applications used for timestamping purposes.
8. Test your network connection
There are several ways to test your network connection in PowerShell. The Test-Connection command checks whether the remote host can be accessed through the network:
Test-Connection -ComputerName techrepublic.com
This will send an ICMP echo request to TechRepublic.com and report its reachability and round-trip time in milliseconds. You can also replace the URL with the IP address of the device.
You can test port availability using the command Test-NetConnection
also:
Test-Connection -ComputerName techrepublic.com -Port 80
This will check if port 80 on techrepublic.com is accessible and if so, use TcpTestSucceeded
The output of True
. Without adding a new port number, this command will verify DNS resolution, that is, whether the domain name can be resolved to the IP address of the remote host.
You can also use the traditional ping
Use URL or IP commands in PowerShell for network testing.
9. Retrieve file hash value
Retrieving file hashes is useful for verifying the integrity of a file. By comparing a file’s hash value to a known reference, you can ensure that it has not been altered, corrupted, or malicious. To retrieve the archive hash in PowerShell, you can use Get-FileHash
command and -Algorithm
scope:
Get-FileHash -Path “N:\Data\Report.txt” -Algorithm SHA1
If you do not define a cryptographic hash algorithm, SHA256 is used by default.
10. Stop and smell the roses
In PowerShell, the results of certain commands scroll quickly across the screen. If you don’t export the results to a file, you may not be able to see on-screen interactions.
let’s use again Get-ChildItem
command from the previous example. This command can return many results, depending on the contents of your path. We will create a function called EasyView
Displays one line every half second for easy viewing of results on screen. The EasyView function will be created as follows:
function EasyView { process { $_; Start-Sleep -seconds .5}}
The $_ represents the current object being processed in the pipeline
. To build a PowerShell command, use an EasyView function, call it using a pipe at the end of the command, and then use the function name, as follows:
Get-ChildItem N:Data | EasyView
The EasyView function is configured to display rows at half-second intervals. You can also use milliseconds for this value.
Rick Vanover contributed to this article.
2024-12-12 21:00:21