10 Quick Microsoft Windows PowerShell Ninja Tricks!

It is important to learn how to use PowerShell for daily use because PowerShell is definitely here to stay for the long term. It is a core part of Exchange 2019, Windows Server 2019, and SQL Server 2019, and it has enormous power we all need to understand.

Below are a few hacks that will prove useful. Knowing how to use these hacks, you will be able to show you have the solution from the command line. Having someone watch you right-click and fix something does not have the same appeal.

Note: Always protect yourself and establish a test environment for your learning experiences. Also consider using the "-confirm" parameter to test configurations before execution for certain commands.


1) Report all of the USB devices installed

PowerShell is Windows Management Instrumentation (WMI) aware. From PowerShell, you can make a WMI call to retrieve the USB devices installed in a local or remote system:

gwmi Win32_USBControllerDevice -computername SERVER1 |fl Antecedent,Dependent

This will apply a filter to bring back the antecedent and dependent fields from the SERVER1 computer. Should you want the full export, you can omit the pipe and filter statement to have a comprehensive export of the USB devices on a system. I have found this useful to maintain a report for servers that have a USB license device installed so that their connectivity is maintained from the device perspective.

2) Perform your favorite CMD tasks in PowerShell

Yes, you can stop using the DOS prompt and start doing all of those same tasks within PowerShell. This can make learning a little easier and help you become more familiar with the interface. Unfortunately, from the run prompt, there is no three-letter launcher like cmd. But powershell will launch it. You can also assign a shortcut key to PowerShell so Ctrl + Shift + P launches it directly.

3) Kill a process in PowerShell instead of Task Manager

When you have a Windows service running that will not respond to stop commands, you can use PowerShell to perform the equivalent actions of ending the task within Task Manager. For instance, you'd do the following for JunkProcess.exe:

get-process JunkPro*

Once the Process ID has been identified, you can kill the errant process by entering:

stop-process -id 2322

At that point, the JunkProcess example will be hard stopped and you can resume your attempt to start the service. You can do that right here in PowerShell as well.

4) Use PSDrive to view more than just drives

The PSDrive command lets you view objects of the Windows environment beyond traditional network, local, or removable drives. One popular view is the HKLM PSDrive to view the HKEY_LOCAL_MACHINE top-level hive of the registry. To get into the registry, enter the following command:

PS C:> cd HKLM:

PS HKLM:/>

You are then transported into the registry hive and can view and even delete items, should you wish.

5) Export NTFS folder permissions -- recursive or not

Managing NTFS permissions is a separate matter, but with PowerShell, you can export the permissions to audit access or take a quick look at access control lists (ACLs) for the security configuration. This can be a great accountability mechanism to run in a scripted format periodically -- or you can run it on demand to diagnose a particular issue. For example, take the following iteration:

PS E:>Get-Acl N:Data

This will give you a quick report of your security rights to the specified path (note that it will not give the share access). That alone is nothing too exciting, as it will report only the single specified path, but if you want to include recursion for the entire path, you can use other strategies. For the same path (N:\Data), you would use the Get-ChildItem command (cmdlet) within PowerShell, combined with the Get-Acl command. Consider the following example:

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

This will span the entire N:\Data path and display the ACLs for the contents of the path. What happens here is that the Get-ChildItem provides an inventory of the file system objects, and that collection is passed to Get-Acl to provide the results for each item.

If you want to archive this to a comma-separated variable (CSV) document, you pass "| export-csv c:\filename.csv" at the end of the cmdlet. You can also pass the normal "> C:\filename.txt" to the end of the command to get it exported to a text file. Note that when you use the -recurse option, it does just that and will traverse the entire path you specify. So be careful when doing it across a large volume or over the network.

6) Play with PowerShell 2.0

PowerShell 2.0 is in the Community Technology Preview (CTP) stage. It includes a graphical interface, Graphical PowerShell, and it is cool. The PowerShell scripts are saved as .ps1 files, making it easy to modify, import, and transfer scripts across systems.

One note on PowerShell 2.0: You have to configure the execution policy through PowerShell (non-graphical version) before using the tool. Configure one of the following execution policies:

PS C:>Set-ExecutionPolicy Restricted (check only)

PS C:>Set-ExecutionPolicy AllSigned (most secure)

PS C:>Set-ExecutionPolicy RemoteSigned (medium secure)

PS C:>Set-ExecutionPolicy Unrestricted (least secure)

When deciding to evaluate PowerShell 2.0, note that the WS-MAN v1.1 (or above) package is required, and if you want to use the graphical interface, Microsoft .NET Framework 3.0 (or above) is required.

7) Work from the keyboard in Graphical PowerShell

If you are familiar with the Microsoft SQL Query Analyzer environment, you will appreciate some of these keyboard shortcuts. In Graphical PowerShell, you can select a single line or multiple lines and execute them by pressing the F5 key. Also, if you have modified your script, the familiar Ctrl + S to save, Ctrl + Z to undo, Ctrl + C to copy, and Ctrl + V to paste are available to save you time in the editing and testing.

8) Background a time-consuming task

If you have a cmdlet that will take some time to run, you can use PowerShell to send it to the background to complete. In this way, you can send a series of commands to execute at once and let them complete on their own schedule. The command to launch a background job leads with the start-psjob -command parameter. You can query PowerShell on the status of any of the jobs with the following command:

PS C:>get-psjob

You will see a table of results showing the current-status of your jobs, with a session identifier that is unique for each job. You can remove the failed job by running the following command:

PS C:>remove-psjob 9

9) Insert timestamps into PowerShell outputs

For your PowerShell tasks, you can have a timestamp entered in series so you can determine how long a single step occurs or to use as a logging mechanism for your scripts. I find this handy in Graphical PowerShell when I'm testing scripts. To insert a timestamp, enter one of the following commands as a single line within your .ps1 file:

Command
Output example
"$(Get-Date -format g) Start logging"
9/21/2020 8:23 AM
"$(Get-Date -format F) Start logging"
Monday, September 21, 2020 8:23:46 AM
“$(Get-Date -format o) Start logging"
2020-09-21T08:23:46.0368750-05:00

There are many other formats for the Get-Date command, but these three options would generally suite most applications for timestamp purposes.

10) Take the EasyView route

Within PowerShell, some commands have results that scroll through the screen very quickly. If you are not exporting the results to a file, it may be impossible to view the onscreen interaction. Let us again use the Get-ChildItem command from previous example. This command can return many results depending on your path contents. We will create a function called EasyView to make it easy to view the results onscreen by displaying one line every half-second. The EasyView function can be created as follows:

function EasyView { process { $_; Start-Sleep -seconds .5}}

To make a PowerShell command use the EasyView function, call it with a pipe at the end of the command and then the function name as shown below:

Get-ChildItem N:Data | EasyView

The EasyView function is configured to display lines at a half-second interval. You can also use milliseconds for the value.


Implementing these command-line hacks in your day to day routine will show your employer that you are a real IT Ninja! The ability to know real world application has never been more necessary. Learn this and much more by enrolling in the Global IT Training Powershell Ulitmate Bootcamp today!


Special Offer!

UP TO $500 OFF!*

Your Choice of Discount Off Class,

or Receive as a Visa Gift Card!

Group Rates and Government Employer Discounts also Available!

*Offer valid for paid enrollments through 12/31/2024. Contact us for complete details.

Ready to Jumpstart Your IT Career?

CONTACT US NOW!