Get
get-command PS C:\> get-command
| Retrieves information.
For example, the get-command cmdlet retrieves a listing of all PowerShell commands. |
Set
set-executionpolicy policy PS C:\> set-executionpolicy remotesigned
| Configures a setting.
The example sets the PowerShell execution policy to remote signed, allowing local scripts to run. |
Format
format-table columns PS C:\> get-service | format-table Name, Status, DependentServices -auto
| Formats the output.
The example gets a listing of all services but picks specific columns to include in the table.
Tip
The pipe command (|) sends the output from the previous command to the next command. In other words, the output if get-service is sent to format-table.
Tip
The -auto switch helps the output fit on the screen.
|
Out
Out-file filename PS C:\> get-process | out-file processes.txt PS C:\> get-process > processes.txt
| Sends data out to a file.
The example uses get-process to retrieve a listing of all running processes and sends it to a text file named processes.txt. You can achieve the same result using the redirect symbol (>), as shown in the second example. |
Gridview
Get-service | out-gridview PS C:\> get-service | where-object {$_.status -eq "running"} | sort-object -property displayname | out-gridview
| Sends the output to a grid view window similar to Figure 1.
The example command uses the where-object cmdlet to list only running services, the sort-object cmdlet to sort on the displayname, and the out-gridview cmdlet to send the output to a gridview window.
Tip
The gridview window allows dynamic sorting by simply clicking on any column to reorder the data.
Note
The gridview requires the Microsoft .NET Framework 3.5 Service Pack 1 (available at http://go.microsoft.com/fwlink?linkid=124150). On Windows Server 2008 R2, it requires that the Windows Integrated Scripting feature be installed via Server Manager.
|
Selected columns in gridview
Get-process | select columns | out-gridview PS C:\> get-process | select-object name, description, handles, vm, ws, pm, npm, cpu, totalprocessortime | out-gridview
| This example shows how to select specific columns in the gridview by using the select-object cmdlet. The output of this command is shown in Figure 2.
Tip
You can view a list of all selectable columns (and their name in the header) by running get-process | select-object * | out-gridview.
|
Test
test-path path PS C:\> test-path c:\data\ processes.txt
| Tests
various conditions like the existence of a file or the existence of a
registry key.
The example tests to see whether the path and filename exist. If it
exists, it returns a true. If it doesn’t exist, it returns a false. |
Write
write-warning message PS C:\> write-warning "Success!"
| Writes data to various sources such as the screen or event logs.
The example writes the message to the screen in yellow text. |
Start
start-service servicename PS C:\> start-service msftpsvc
| You can use this to start jobs, processes, services, and more.
The example starts the FTP Publishing Service using the service’s name.
Tip
You can get a list of all services (including their service name) with the get-serviceName column.
command. The service name is displayed in the
|
Stop
stop-service servicename PS C:\> stop-service msftpsvc
| You can use this to stop jobs, processes, services, and more.
The example stops the FTP Publishing Service using the service’s name. |
Measure
measure-object PS C:\> get-command | measure-object
| Provides
counts, averages, and other metrics for various objects.
The example command counts the number of Windows PowerShell commands
and provides the total. There are more than 400 built-in commands. |