Technology

10 Interesting and Useful PowerShell Commands to Improve Productivity

10 Interesting and Useful PowerShell Commands to Improve Productivity

If PowerShell’s studying curve has stored you from embracing it, “cool” is probably not a phrase you’d affiliate with it. But PowerShell is right here to remain. It is a core a part of Microsoft 365, Azure and Windows Server 2022 and has immense energy.

In this text, TechRepublic will supply some methods which may turn out to be useful. Plus, it is all the time nicer to shock somebody with the command line resolution. Having somebody watch you right-click and repair one thing does not have the identical enchantment.

Note: Be cautious, very cautious

Yes, this can be a software worthy of the identify. PowerShell can simply trigger big configuration modifications, optimistic or damaging, so defend your self and create a testing setting in your studying experiences. Also, think about using -confirm parameter to check configurations earlier than executing sure instructions.

1. Report all put in USB units

PowerShell is suitable with Windows Management Instrumentation. From PowerShell you may make a WMI name to retrieve USB units put in on a neighborhood or distant system:

gwmi Win32_USBControllerDevice -computername SERVER1 |fl Antecedent,Dependent

This filter will report fields previous to and depending on the SERVER1 laptop. If you need the total export, you’ll be able to omit the pipe, | and the filter assertion to completely export USB units on a system.

This could also be helpful for sustaining a report for servers with a USB licensing gadget put in to keep up connectivity from a tool perspective.

2. Perform your favourite Command Prompt duties

All duties carried out within the Command Prompt will also be carried out inside PowerShell. This might make it easier to turn out to be extra acquainted with the interface.

Launch PowerShell within the Run with command dialog powershell. You may also assign a hotkey to PowerShell in order that Ctrl+Shift+P launches it straight.

3. Kill a course of in PowerShell as a substitute of Task Manager

When you will have a Windows service working that does not reply to cease instructions, you should utilize PowerShell to carry out actions equal to ending the duty in Task Manager. For instance, you’ll do the next for BadThread.exe:

get-process BadTh*

The outcomes will appear to be this:

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

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

28 4 -210844 -201128 -163 25.67 2792 BadThread

Once you’ve got recognized the method ID, you’ll be able to kill the errant course of by getting into:

stop-process -id 2792

At that time, the BadThread instance will cease completely and you may resume making an attempt to begin the service. You may also do that right here in PowerShell.

But, if the method does not end efficiently, you should utilize the -Force parameter:

stop-process -id 2792 -Force

Be cautious when utilizing it, as it might trigger information loss or corruption if the method is in the course of a job.

4. Use PSDrive to view extra than simply drives

The PSDrive command lets you view objects in your Windows setting past conventional community, native, or detachable drives. A preferred view is HKLM PSDrive, which lets you view the HKEY_LOCAL_MACHINE top-level hive of the registry. To entry the registry, enter the next command:

PS C:> cd HKLM:

PS HKLM:/>

You will then be transported to the registry hive and may view and even delete gadgets if you want.

5. NTFS export folder permissions: recursive or not

Managing NTFS permissions is a separate concern, however with PowerShell you’ll be able to export permissions to regulate entry or take a fast take a look at entry management lists for safety configuration. This is usually a nice accountability mechanism to run periodically in a script-based format, or you’ll be able to run it on demand to diagnose a selected downside.

For instance, take the next iteration:

PS E:>Get-Acl N:Data

This will provide you with a fast report in your safety rights to the desired path (observe that it’ll not present shared entry). This alone is nothing too thrilling, as it’ll solely report the only specified path. But if you wish to embody recursion for all the path, you should utilize different methods.

For the identical path N:Data, you’ll use the file Get-BabyItem command inside PowerShell, mixed with the command Get-Acl command. Consider the next instance:

PS E:>Get-BabyItem N:Data -recurse | Get-Acl

This will cowl all the path N:Data and show the ACLs for the contents of the trail. What occurs right here is that the Get-BabyItem gives a list of file system objects to which the gathering is handed Get-Acl to offer outcomes for every merchandise.

If you need to retailer it in a comma separated variables (CSV) doc, you move | export-csv c:filename.csv on the finish of the command. You may also exceed the traditional > C:filename.txt on the finish of the command to export it to a textual content file.

Please observe that whenever you use the -recurse choice, it does simply that and can journey all the specified path. So watch out when doing this on a big quantity or over the community.

6. Process a time-consuming job within the background

If a command or cmdlet takes some time to run, you should utilize PowerShell to ship it to the background to finish. This manner, you’ll be able to ship a collection of instructions to run without delay and allow them to full on their very own schedule.

The command to begin a background job brings with the file start-psjob parameter. You can question PowerShell concerning the standing of any job 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 every of them. You can take away any failed processes by working the next command:

PS C:>remove-psjob 9

7. Insert timestamps into PowerShell outputs

For PowerShell duties, you’ll be able to insert a serial timestamp to find out the length of a single step or use it as a logging mechanism in your scripts.

To insert a timestamp, enter one of many following instructions on a single line throughout the .ps1 file:

Date formatCommandExit instance
Brief basic (g)$(Get-Date -format g) Start logging12/12/2024 9:15 PM
Full date/time (F)$(Get-Date -format F) Start loggingThursday, December 12, 2024 9:15:13 PM
Round journey (o)$(Get-Date -format o) Start logging2024-12-12T21:15:13.0368750-05:00

There are many different codecs for the file Get-Date command, however these three choices usually swimsuit most functions for timestamping functions.

8. Check your community connection

There are a number of methods to check your community connection in PowerShell. The Test-Connection command checks whether or not a distant host is reachable on the community:

Test-Connection -ComputerName techrepublic.com

This will ship ICMP echo requests to TechRepublic.com and report whether or not it’s reachable and the round-trip time in milliseconds. You may also change the URL with a tool’s IP deal with.

You can check the provision of the port with the command Test-WebConnectionas nicely:

Test-Connection -ComputerName techrepublic.com -Port 80

This checks whether or not port 80 on techrepublic.com is reachable and, if that’s the case, will point out this with a TcpTestSucceeded exit of True. Without including a port quantity, this command will verify DNS decision, i.e. whether or not the area identify could be resolved to the IP deal with of a distant host.

You may also use the standard one ping command with a URL or IP inside PowerShell for community testing.

9. Retrieve a file hash

Retrieving the hash of a file is helpful for verifying the integrity of the file. By evaluating a file’s hash to a identified reference worth, you’ll be able to be sure that it’s not altered, corrupted, or malicious. To retrieve a file hash in PowerShell, you should utilize the Get-FileHash command e -Algorithm parameter:

Get-FileHash -Path “N:DataReport.txt” -Algorithm SHA1

If you don’t outline a cryptographic hash algorithm, SHA256 can be assumed by default.

10. Stop and scent the roses

Inside PowerShell, some instructions have outcomes that scroll throughout the display in a short time. If you do not export the outcomes to a file, you could not be capable to see the interplay on display.

We nonetheless use the Get-BabyItem command from the earlier instance. This command can return many outcomes relying on the contents of the trail. We will create a perform known as EasyView to make it simpler to view the outcomes on the display by displaying a line each half second. The EasyView characteristic could be created as follows:

perform EasyView { course of { $_; Start-Sleep -seconds .5}}

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

Get-BabyItem N:Data | EasyView

The EasyView characteristic is configured to show strains at half-second intervals. You may also use milliseconds for the worth.

Rick Vanover contributed to this text.

Source Link

Shares:

Related Posts

Leave a Reply

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