10 Useful Powershell commands that everyone should know

Useful Powershell commands

Many developers adore PowerShell, and with good reason: it enhances the Windows Command Prompt, where many of us spend a significant amount of time. It does, however, have a learning curve, but once you’ve mastered the essential instructions, it’ll be productivity on steroids for you.

Cmdlets are the driving force behind PowerShell’s functional capabilities. There are dozens of critical commands that developers should know, ranging from instructions that improve the general Windows experience to ones that are useful for development work. This list has been compiled to serve as a useful reference tool for individuals who are just getting started.

Get-Help

For anyone using PowerShell, the Get-Help command is crucial, as it provides instant access to the information you need to run and operate with all of the available commands.

Below is the example.

				
					Get-Help [[-Name] <String>] [-Path <String>] [-Category <String[]>] [-Component <String[]>]
[-Functionality <String[]>] [-Role <String[]>] [-Examples] [<CommonParameters>]
				
			

Get-Command

Get-Command is a handy reference cmdlet that displays all of the commands that are accessible in your current session.

				
					get-command
				
			

Output looks like this:

				
					CommandType     Name                            Definition
-----------     ----                            ----------
Cmdlet          Add-Content                     Add-Content [-Path] <String[...
Cmdlet          Add-History                     Add-History [[-InputObject] ...
Cmdlet          Add-Member                      Add-Member [-MemberType]
				
			

Set-ExecutionPolicy

To prevent malicious scripts from running in the PowerShell environment, Microsoft disables scripting by default. Developers, on the other hand, want to be able to build and run scripts, therefore the Set-ExecutionPolicy command lets you adjust the level of security for PowerShell scripts. You can choose from four different security levels:

Restricted: This is the default security level, which prevents the execution of PowerShell scripts. You can only enter commands interactively at this security level.

All Signed: This security level only allows scripts to run if they have been signed by a reliable publisher.                       

Remote Signed: Any PowerShell scripts produced locally are allowed to run at this security level. Remotely developed scripts are only allowed to run if they have been signed by a recognized publisher.

Unrestricted: As the name implies, the unrestricted security level removes all limitations from the execution policy, allowing all scripts to run.

Get-ExecutionPolicy

Similarly, if you’re working in an unfamiliar environment, this command can quickly reveal the current execution policy:

Get-Service

Knowing what services are installed on the system is also beneficial. With the following command, you may quickly get this data:

				
					Get-Service
				
			

The output might look like:

				
					Status   Name               DisplayName                           
------   ----               -----------                           
Running  AarSvc_4f948d3     Agent Activation Runtime_4f948d3      
Running  AdobeARMservice    Adobe Acrobat Update Service          
Stopped  AJRouter           AllJoyn Router Service                
Stopped  ALG                Application Layer Gateway Service     
Running  AMD Crash Defen... AMD Crash Defender Service            
Running  AMD External Ev... AMD External Events Utility
				
			

If you need to know if a certain service is installed, add the -Name switch to the command and the service’s name, and Windows will display the service’s status. Filtering capabilities can also be used to return a specified subset of currently installed services.

Get-Eventlog

The Get-EventLog cmdlet in PowerShell can really parse your machine’s event logs. There are a number of options accessible. To read a specific log, use the -Log switch followed by the name of the log file. To view the Application log, for example, execute the following command:

				
					Get-EventLog -Log "Application"
				
			

Other parameters is Get-Eventlog are:

  • -Verbose
  • -Debug
  • -ErrorAction
  • -ErrorVariable
  • -WarningAction
  • -WarningVariable
  • -OutBuffer
  • -OutVariable

Get-Process

It’s often handy to be able to receive quick list of all the presently operating processes, similar to receiving list of accessible services. 
 
This information is available with the Get-Process command.
				
					Get-Process
				
			

The output might look like this:

				
					
Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                                                                                                                                       
-------  ------    -----      -----     ------     --  -- -----------                                                                                                                                       
    206      13     2696       4320       0.38  13684   6 AdobeIPCBroker                                                                                                                                    
    110       8     2008       4224              3816   0 amdfendrsr                                                                                                                                        
    371      16     5996      12548              2528   0 AppHelperCap                                                                                                                                      
    502      29    20728      14560       1.64   9688   6 ApplicationFrameHost                                                                                                                              
    124       8     1556       2204              5372   0 armsvc     
				
			

Stop-Process

Stop-Process can be used to terminate processes that have been frozen or are no longer responding. Use Get-Process to rapidly discover the issue process if you’re not sure what’s causing the delay. Stop-Process can be used to stop a process after you have the name or process ID.. Below is an example of the same.
				
					Stop-Process -processname armsvc
				
			

Clear-History

What if you wish to delete all of your command history entries? Using the Clear-History cmdlet is simple. It can also be used to remove only certain commands. For example, the following command will delete commands that begin with “help” or conclude with “command”:
				
					Clear-History -Command *help*, *command
				
			

Convertto-html

If you need to extract data for a report or to distribute to someone else, ConvertTo-HTML is a quick and easy way to do so. To utilise it, feed the output of another command to the ConvertTo-HTML command and specify which output properties you want in the HTML file with the -Property switch. You’ll also have to give the file a name.
For example, the following code creates an HTML page that lists the PowerShell commands in the current console:
				
					 get-commad | convertto-html > command.htm
				
			

Conclusion

Above cmdlets are some of my special mentions which I use in my day-to-day activities and are also used widely by many Powershell developers. Let me know in the comment section what I have missed in this post so that we can cover it in the coming posts.

Leave a Reply

Please disable your adblocker or whitelist this site!