10 USEFUL POWERSHELL SCRIPTS

If PowerShell seems difficult to use for your day-to-day tasks, cool may not be the word you associate with it. Here, we present 10 USEFUL POWERSHELL SCRIPTS in 2021.

PowerShell is a core part of Exchange, Windows Server, and SQL Server, and it has tremendous capabilities that we all need to understand, embrace, and use to make our day-to-day business easier and more automated.

10 COOL THINGS YOU CAN DO WITH WINDOWS POWERSHELL

1: REPORTS OF CONNECTED USB EQUIPMENT

PowerShell makes it possible to work with Windows Management Instrumentation (WMI). Using PowerShell, you can make a WMI query to get information about USB devices that are installed on both local and remote systems.

gwmi Win32_USBControllerDevice – computername DBSERVER1 | fl Antecedent, Dependent 

This command will apply a filter to return preceding and dependent fields from the computer DBSERVER1. If you want to get complete information about USB devices in the system, you can remove the operator |and fl. This is a very convenient way to keep track of the servers to which the licensed USB devices are connected.

2: PERFORMING YOUR FAVORITE CMD TASKS IN POWERSHELL

Yes, you can stop using the Command Prompt (CMD) and start performing all the same tasks in PowerShell. This will help make the learning process a little easier and help you become more familiar with the interface. Unfortunately, PowerShell cannot be invoked through the Run window using three letters like CMD. But you can assign a keyboard shortcut to quickly launch PowerShell, for example, Ctrl + Shift + P.

3: FORCE QUITTING A PROCESS IN POWERSHELL

If a Windows service is stuck, you can use PowerShell to terminate the process in the same way as through the Task Manager. For example, to close BadThread.exe, you do the following:

get – process BadTh * The results will display the data we need in this format: Handles   NPM (K)     PM (K)       WS (K) VM (M)    CPU (s) Id ProcessName ——- —— —– —– —– —— – ———– 19 5 – 321955 – 312219 –

               154 32.76 7583 BadThread        

After we identify Process ID, you can forcefully close the hung process by entering the command:

stop – process – id 7583

At the same time, the BadThread process will be forcibly stopped, and you can resume trying to start the service. Which you can do right here in PowerShell.

4: USE PSDRIVE FOR MORE THAN JUST BROWSING DISCS

The command PSDriveallows you to view Windows objects outside of traditional networks, as well as local or removable drives. For example, to view the drives under the top-level registry key HKEY_LOCAL_MACHINE, you can use HKLM PSDrive. To enter the registry, enter the following command:

PS C :>         cd HKLM : 

PS HKLM: />

Then you switch to the register section where you can view a list of all objects and delete them if you need them.

5: EXPORT NTFS FOLDER PERMISSIONS – AS USUAL, AND RECURSIVELY

Managing NTFS permissions is a separate issue, but you can use PowerShell to export the permission list to audit access or to quickly parse the ACLs to configure security policies. This is the best option for generating reports in the format of a periodically run script, or you can run it on demand, for example, to diagnose a specific access issue. For example, using the following command:

PS E :> Get – Acl N: Data

This will give you a quick response with the results of your security rights at the specified path N: Data (note that the command does not give access to the resource). This command will not give us a general picture of all accesses of the entire path, but only a report only about the specified path. But if you want to include recursion for the entire path, you can use another command. For the same path, N: Data you must use a cmdlet Get-ChildItem (cmdlet) in PowerShell in conjunction with a cmdlet Get-Acl. Consider the following example:

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

This command will display ACLs for the contents of the entire path N: Data. Let’s see how it works: the cmdlet Get-ChildItem shows us all the objects in the file system at the specified path N: Data, and then the entire list of objects is passed to the cmdlet, Get-Acl which provides the results (ACLs) for each object.

If you want to archive data into a document (CSV) you need to add | export-csv c:\filename.csvat the end of the command. Alternatively, you can extract to a plain text file by adding a cmdlet > C:\filename.txt. Note that when you use the -recurse parameter, it will be applied across all attached files and folders. Therefore, be careful when using it to inventory large volumes or over the network.

Also Read: Top 10 PowerShell Commands I wish I knew before 

6: POWERSHELL 2.0 DIFFERENCES

PowerShell 2.0 includes a graphical interface, which is a handy feature of this system. PowerShell scripts are saved as files .ps1, which allows us to easily modify, import, and migrate scripts to various systems. 

Note for PowerShell 2.0: Before you can start using PowerShell 2.0, you must configure the execution policy using the first version of PowerShell. Enter one of the following commands to customize the execution policy for your needs:

The C PS :> the Set – ExecutionPolicy Tel Restricted (only check) 

PS the C :> the Set – ExecutionPolicy AllSigned (most secure) 

PS the C :> the Set – ExecutionPolicy RemoteSigned (average level of security) 

PS the C :> the Set – ExecutionPolicy Unrestricted (less secure)                 

However, remember that PowerShell 2.0 requires WS-MAN v1.1 and Microsoft .NET Framework 3.0 for the GUI.

7: HOTKEYS IN POWERSHELL GUI

If you are familiar with the Microsoft SQL Query Analyzer environment, you will appreciate some of these keyboard shortcuts. In the PowerShell GUI, you can select one or more lines and execute them at once with one keystroke F5. In addition, if you have changed the script, then to save time when editing and testing, the usual ones are available Ctrl + S for saving, Ctrl + Z for undoing, Ctrl + C for copying, and Ctrl + V for pasting.

8: BACKGROUND MODE FOR LONG TASKS

If you are going to use a command that will take a while to complete, you can run PowerShell in the background until it completes. Thus, you can send a series of commands to be automatically executed on your own schedule. To run a command in the background, you need to add a parameter to the beginning –psjob. You can also find out about the status of any of the tasks using the following command:

PS C :> get – psjob 

In an additional window, you will see a table with the results of the current states of your tasks, in addition to unique session IDs for each task separately.  

you can delete the failed task by specifying the Session ID at the end of the command:

PS C :> remove – psjob 9

9: INSERTING TIME FRAMES FOR POWERSHELL COMMAND OUTPUT

For PowerShell tasks, you can enter a sequence timestamp to determine the duration of each step, and you can also use it to customize the log of the scripts you enter. This can be a convenient way to test them. To insert a timestamp, enter one of the following commands as a single line in the file .ps1:

Commands

“$ (Get-Date -format g) Start logging”

“$ (Get-Date -format F) Start logging”

“$ (Get-Date -format o) Start logging”

There are many different command formats Get-Date, but usually, these three options are suitable for most timestamped purposes.

10: DELAYED OUTPUT OF RESULTS

In PowerShell, some commands display information on the screen in quick scrolling. If you do not export the data to a file, it will not be possible to view it on the screen. Let’s use the cmdlet Get-ChildItem from the previous examples again.

This command can output many results depending on the specified path. To make it easier to view the displayed data on the screen, we will use a function called EasyView. This function allows us to view the results on the screen by displaying one line every half second. The EasyView function is created as follows:

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

To execute a PowerShell command using a function, EasyViewadd a statement |and the name of the function at the end of the command, as shown below:

Get – ChildItem N: Data | EasyView  

EasyView is configured to display lines at half-second intervals. You can also adjust the interval in milliseconds.

OUTCOMES

Useful scripts don’t end there at these 10 points. There are tons of PowerShell features that can simplify your daily tasks.