At Host Africa, we believe in helping our clients to the best of our ability. This sometimes also means refusing to help clients as some queries fall outside our area of expertise. Windows Powershell is one of these areas.
As a result, we would like to help with a short article compiled from various internet sources and a touch of own experience.
A few basic PowerShell commands
Get help on any command, for example, the Get-Service command:
Get-Help -Name Get-Service
Get a list of all commands:
Get a list of all commands for use with the Get- verb:
Get-Help -Name Get-*
Find the five processes using the most memory (this will be familiar to most Linux administrators):
ps | sort –p ws | select –last 5
Get and Set script execution policies
Microsoft has disabled scripting by default in an effort to prevent malicious code from executing in a PowerShell environment. To change the existing policy or to find out what it is, you can use the Get-ExecutionPolicy and the Set-ExecutionPolicy commands. Four levels of control are available:
- Restricted — Restricted is the default execution policy and locks PowerShell down so that commands can be entered only interactively. PowerShell scripts are not allowed to run.
- All Signed — If the execution policy is set to All Signed then scripts will be allowed to run, but only if they are signed by a trusted publisher.
- Remote Signed — If the execution policy is set to Remote Signed, any PowerShell scripts that have been locally created will be allowed to run. Scripts created remotely are allowed to run only if they are signed by a trusted publisher.
- Unrestricted — As the name implies, Unrestricted removes all restrictions from the execution policy.
To change a policy, enter the Set-ExecutionPolicy command followed by the policy name. If you want all scripts to run with NO restriction, enter the following:
Control a thing or two with PowerShell commands
To restart a service such as DHCP, execute the following:
To delete all the files in a directory without being prompted for confirmation:
Remove-Item C:\tobedeleted –Recurse
To restart the computer you are logged in to:
(Get-WmiObject -Class Win32_OperatingSystem -ComputerName .).Win32Shutdown(2)
Create some useful output
PowerShell can provide a wealth of information about the system, but sometimes you need to do more than just view the information onscreen. One way of accomplishing this is by using the ConvertTo-HTML command. To use this command, simply pipe the output from another command into the ConvertTo-HTML command. You will have to use the -Property switch to control which output properties are included in the HTML file and you will have to provide a filename.
For example, to get a list of all installed services and output the name of the service and the status of that service to a web-page that you can view using your browser, enter the following:
Get-Service | ConvertTo-HTML -Property Name, Status > C:\services.htm
See what’s happening
Just as you can use the Get-Service command to display a list of all of the system services, you can use the Get-Process command to display a list of all of the processes that are currently running on the system. You can also use another Process command to stop an unresponsive process as in the following example:
Stop-Process -Name notepad
You can also get detailed processor info:
Get-Processor -Computer SRV1.MYMACHINE.DAT
I hope these few commands have encouraged you to explore the world of Windows PowerShell.