10 Cool and Useful PowerShell Commands to Enhance Productivity

10 Cool and Useful Powershell Commands to Enhance Productivity

10 Cool and Useful PowerShell Commands to Enhance Productivity

Home ยป News ยป 10 Cool and Useful PowerShell Commands to Enhance Productivity
Table of Contents

If PowerShellโ€™s studying curve has saved you from embracing it, โ€œcoolโ€ won’t be a phrase youโ€™d affiliate with it. However PowerShell is right here to remain. Itโ€™s a core a part of Microsoft 365, Azure, and Home windows Server 2022 and has immense energy.

On this article, roosho will provide just a few tips that would turn out to be useful. Moreover, it’s all the time cooler if you amaze somebody with the answer from the command line. Having somebody watch you right-click and repair one thing doesnโ€™t have the identical attraction.

Notice: Watch out, very cautious

Sure, it is a software worthy of the identify. PowerShell can simply trigger huge configuration adjustments, optimistic or damaging โ€” so defend your self and set up a take a look at surroundings on your studying experiences. Additionally, think about using the -confirm parameter to check configurations earlier than execution for sure instructions.

1. Report the entire USB gadgets put in

PowerShell is Home windows Administration Instrumentation conscious. From PowerShell, you can also make a WMI name to retrieve the USB gadgets put in in a neighborhood or distant system:

gwmi Win32_USBControllerDevice -computername SERVER1 |fl Antecedent,Dependent

This filter will carry again the antecedent and dependent fields from the SERVER1 pc. Do you have to need the total export, you may omit the pipe, | , and filter assertion to comprehensively export the USB gadgets on a system.

This might be helpful for sustaining a report for servers with a USB license machine put in to keep up connectivity from the machineโ€™s perspective.

2. Carry out your favourite Command Immediate duties

All duties carried out within the Command Immediate can be accomplished inside PowerShell. This might assist you change into extra conversant in the interface.

Launch PowerShell within the Run dialog field with the command powershell. You can even assign a shortcut key to PowerShell so Ctrl + Shift + P launches it straight.

3. Kill a course of in PowerShell as an alternative of Process Supervisor

When you might have a Home windows service operating that won’t reply to cease instructions, you should use PowerShell to carry out the equal actions of ending the duty inside Process Supervisor. For example, youโ€™d do the next for BadThread.exe:

get-process BadTh*

The outcomes can be just like this:

Handles NPM(Ok) PM(Ok) WS(Ok) VM(M) CPU(s) Id ProcessName

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

28 4 -210844 -201128 -163 25.67 2792 BadThread

As soon as the Course of ID has been recognized, you may kill the errant course of by coming into:

stop-process -id 2792

At that time, the BadThread instance can be exhausting stopped and you’ll resume your try to begin the service. You are able to do that proper right here in PowerShell as nicely.

However, if the method doesnโ€™t terminate gracefully, you should use the -Drive parameter:

stop-process -id 2792 -Drive

Be cautious when utilizing it, because it may lead to information loss or corruption if the method is in the midst of a job.

4. Use PSDrive to view extra than simply drives

The PSDrive command helps you to view objects of the Home windows surroundings past conventional community, native, or detachable drives. One fashionable view is the HKLM PSDrive, which lets you view the HKEY_LOCAL_MACHINE top-level hive of the registry. To get into the registry, enter the next command:

PS C:> cd HKLM:

PS HKLM:/>

You’re then transported into the registry hive and may view and even delete objects, do you have to want.

5. Export NTFS folder permissions โ€” recursive or not

Managing NTFS permissions is a separate matter, however with PowerShell, you may export the permissions to audit entry or take a fast have a look at entry management lists for the safety configuration. This generally is a nice accountability mechanism to run in a scripted format periodically โ€” or you may run it on demand to diagnose a specific situation.

For instance, take the next iteration:

PS E:>Get-Acl N:Knowledge

This offers you a fast report of your safety rights to the required path (observe that it gainedโ€™t give the shared entry). That alone is nothing too thrilling, as it’ll report solely the only specified path. However if you wish to embody recursion for the complete path, you should use different methods.

For a similar N:Knowledge path, youโ€™d use the Get-ChildItem command inside PowerShell, mixed with the Get-Acl command. Think about the next instance:

PS E:>Get-ChildItem N:Knowledge -recurse | Get-Acl

This can span the complete N:Knowledge path and show the ACLs for the contents of the trail. What occurs right here is that the Get-ChildItem gives a list of the file system objects, and that assortment is handed to Get-Acl to supply the outcomes for every merchandise.

If you wish to archive this to a comma-separated variable (CSV) doc, you cross | export-csv c:filename.csv on the finish of the command. You can even cross the traditional > C:filename.txt to the top of the command to get it exported to a textual content file.

Notice that if you use the -recurse possibility, it does simply that and can traverse the complete path you specify. So watch out when doing it throughout a big quantity or over the community.

6. Background a time-consuming job

If in case you have a command, or cmdlet, that can take a while to run, you should use PowerShell to ship it to the background to finish. On this means, you may ship a collection of instructions to execute directly and allow them to full on their schedule.

The command to launch a background job leads with the start-psjob parameter. You may question PowerShell on the standing of any of the roles with the next command:

PS C:>get-psjob

Youโ€™ll see a desk of outcomes displaying the standing of your jobs, with a novel session identifier for each. You may take away any failed jobs by operating the next command:

PS C:>remove-psjob 9

7. Insert timestamps into PowerShell outputs

To your PowerShell duties, you may have a timestamp entered in collection to find out how lengthy a single step happens or use it as a logging mechanism on your scripts.

To insert a timestamp, enter one of many following instructions as a single line inside your .ps1 file:

Date format Command Output instance
Normal quick (g) $(Get-Date -format g) Begin logging 12/12/2024 9:15 PM
Full date/time (F) $(Get-Date -format F) Begin logging Thursday, December 12, 2024 9:15:13 PM
Spherical journey (o) $(Get-Date -format o) Begin logging 2024-12-12T21:15:13.0368750-05:00

There are numerous different codecs for the Get-Date command, however these three choices would usually swimsuit most purposes for timestamp functions.

8. Check your community connection

There are a number of methods to check your community connection in PowerShell. The Check-Connection command checks if a distant host is reachable over the community:

Check-Connection -ComputerName techrepublic.com

This can ship ICMP Echo Requests to roosho.com and report whether or not itโ€™s reachable and the round-trip time in milliseconds. You can even exchange the URL with a toolโ€™s IP handle.

You may take a look at port availability with the command Check-NetConnection, too:

Check-Connection -ComputerName techrepublic.com -Port 80

This checks if port 80 on techrepublic.com is reachable and, in that case, will denote this with a TcpTestSucceeded output of True. With out including a port quantity, this command will confirm DNS decision โ€” i.e., whether or not the area identify will be resolved to a distant hostโ€™s IP handle.

You can even use the standard ping command with a URL or IP inside PowerShell for community testing.

9. Retrieve a file hash

Retrieving a file hash is helpful for verifying the fileโ€™s integrity. By evaluating the hash of a file to a identified reference worth, you may guarantee it isn’t altered, corrupted, or malicious. To retrieve a file hash in PowerShell, you should use the Get-FileHash command and -Algorithm parameter:

Get-FileHash -Path โ€œN:DataReport.txtโ€ -Algorithm SHA1

In the event you donโ€™t outline a cryptographic hash algorithm, SHA256 can be assumed as default.

10. Cease and odor the roses

Inside PowerShell, some instructions have outcomes that scroll by way of the display in a short time. If you’re not exporting the outcomes to a file, it might be inconceivable to view the onscreen interplay.

Letโ€™s once more use the Get-ChildItem command from the earlier instance. This command can return many outcomes relying in your path contents. Weโ€™ll create a operate referred to as EasyView to make it simple to view the outcomes onscreen by displaying one line each half-second. The EasyView operate could be created as follows:

operate EasyView course of $_; Begin-Sleep -seconds .5

The $_ represents the present object being processed within the pipeline. To make a PowerShell command, use the EasyView operate, name it with a pipe on the finish of the command, after which the operate identify as proven under:

Get-ChildItem N:Knowledge | EasyView

The EasyView operate is configured to show strains at a half-second interval. You can even use milliseconds for the worth.

Rick Vanover contributed to this text.

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.ย 
share this article.

ADVERTISEMENT

ADVERTISEMENT

Enjoying my articles?

Sign up to get new content delivered straight to your inbox.

Please enable JavaScript in your browser to complete this form.
Name