PowerShell Quick Start

You know you are going to need to learn it sooner rather then later.

I use PowerShell everyday for my job, and love using it. It's faster. It's easier to write.

An extra bonus, if you are familiar with C# you can utilize .NET to assist in some situations.

I am going to show you what PowerShell cmdlets I use on a daily basis that help me find what I need within PowerShell.

Like you, at first, I had no idea what I was doing, but PowerShell is easier then you think.

Technically, you only need to remember a few commands and you can reference or search to see if a command or documentation exists in PowerShell.

Here are the commands you should remember:

  1. Get-Help
  2. Get-Command
  3. Get-Member

Get-Help

This PowerShell cmdlet is the one I use the most. If I am unsure of how to do something or to see if something exists within PowerShell I use the Get-Help cmdlet. You can use the cmdlet to view documentation on a specific cmdlet or to receive a list of available cmdlets that contain a keyword.

Here is an example of how I use the Get-Help cmdlet.

Get-Help *CM* # Will list the available ConfigMgr cmdlets available.

Example Out-put

PS > Get-Help *CM*

Name Category Module Synopsis
---- -------- ------ --------
gcm Alias Get-Command
shcm Alias Show-Command
trcm Alias Trace-Command
icm Alias Invoke-Command
.... # This is only the first 4 items that populated out of many. The search came up with so many I ended up force stopping the process with the Ctrl + C

Here is a specific example of how to use the Get-Help cmdlet.

Say your boss has tasked you with deleting certain file extensions within a directory. He or she tells you there is about 1,000 plus files, and you need to comb through the directory to ensure the files containing these specified extensions are removed.

You don't have time to complete a one-by-one search for these file or even have time to complete a *.extension search through the directory.

You go back to your workstation and you say "Self, I wonder if PowerShell can help."

So, to see if PowerShell can help you can open a PowerShell window (elevated or not, dependent upon the situation) to find out.

Does PowerShell have the ability to delete? Let's find out

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS > Get-Help *Delete*

Name Category Module Synopsis
---- -------- ------ --------
Clear-Host Function Clears the display in the host program.
Add-History Cmdlet Microsoft.PowerShell.Core Appends entries to the session history.
Clear-History Cmdlet Microsoft.PowerShell.Core Deletes entries from the command history.
Disconnect-PSSession Cmdlet Microsoft.PowerShell.Core Disconnects from a session.
Enable-PSSessionConfiguration Cmdlet Microsoft.PowerShell.Core Enables the session configurations on the local computer.
Enter-PSSession Cmdlet Microsoft.PowerShell.Core Starts an interactive session with a remote computer.
Get-Job Cmdlet Microsoft.PowerShell.Core Gets Windows PowerShell background jobs that are running in the current session.
Get-PSSession Cmdlet Microsoft.PowerShell.Core Gets the Windows PowerShell sessions on local and remote computers.
New-PSSession Cmdlet Microsoft.PowerShell.Core Creates a persistent connection to a local or remote computer.
Out-Null Cmdlet Microsoft.PowerShell.Core Deletes output instead of sending it down the pipeline.
Receive-Job Cmdlet Microsoft.PowerShell.Core Gets the results of the Windows PowerShell background jobs in the current session.
Receive-PSSession Cmdlet Microsoft.PowerShell.Core Gets results of commands in disconnected sessions.
Remove-Job Cmdlet Microsoft.PowerShell.Core Deletes a Windows PowerShell background job.
Remove-Module Cmdlet Microsoft.PowerShell.Core Removes modules from the current session.
Remove-PSSession Cmdlet Microsoft.PowerShell.Core Closes one or more Windows PowerShell sessions (PSSessions).
Set-PSSessionConfiguration Cmdlet Microsoft.PowerShell.Core Changes the properties of a registered session configuration.
Stop-Job Cmdlet Microsoft.PowerShell.Core Stops a Windows PowerShell background job.
Unregister-PSSessionConfiguration Cmdlet Microsoft.PowerShell.Core Deletes registered session configurations from the computer.
Update-Help Cmdlet Microsoft.PowerShell.Core Downloads and installs the newest help files on your computer.
Disable-PSRemoting Cmdlet Microsoft.PowerShell.Core Prevents remote users from running commands on the local computer.
Remove-PSSnapin Cmdlet Microsoft.PowerShell.Core Removes Windows PowerShell snap-ins from the current session.
Get-PSReadlineKeyHandler Cmdlet PSReadline Gets the key bindings for the PSReadline module.
Clear-Variable Cmdlet Microsoft.PowerShell.U... Deletes the value of a variable.
ConvertFrom-Csv Cmdlet Microsoft.PowerShell.U... Converts object properties in comma-separated value (CSV) format into CSV versions of the original objects.
Export-PSSession Cmdlet Microsoft.PowerShell.U... Imports commands from another session and saves them in a Windows PowerShell module.
Get-Alias Cmdlet Microsoft.PowerShell.U... Gets the aliases for the current session.
Get-EventSubscriber Cmdlet Microsoft.PowerShell.U... Gets the event subscribers in the current session.
Import-Csv Cmdlet Microsoft.PowerShell.U... Creates table-like custom objects from the items in a CSV file.
Invoke-RestMethod Cmdlet Microsoft.PowerShell.U... Sends an HTTP or HTTPS request to a RESTful web service.
Invoke-WebRequest Cmdlet Microsoft.PowerShell.U... Gets content from a web page on the Internet.
New-Alias Cmdlet Microsoft.PowerShell.U... Creates a new alias.
New-Event Cmdlet Microsoft.PowerShell.U... Creates a new event.
New-Variable Cmdlet Microsoft.PowerShell.U... Creates a new variable.
Out-GridView Cmdlet Microsoft.PowerShell.U... Sends output to an interactive table in a separate window.
Register-EngineEvent Cmdlet Microsoft.PowerShell.U... Subscribes to events that are generated by the Windows PowerShell engine and by the New-Event cmdlet.
Register-ObjectEvent Cmdlet Microsoft.PowerShell.U... Subscribes to the events that are generated by a Microsoft .NET Framework object.
Remove-Event Cmdlet Microsoft.PowerShell.U... Deletes events from the event queue.
Remove-PSBreakpoint Cmdlet Microsoft.PowerShell.U... Deletes breakpoints from the current console.
Remove-TypeData Cmdlet Microsoft.PowerShell.U... Deletes extended types from the current session.
Remove-Variable Cmdlet Microsoft.PowerShell.U... Deletes a variable and its value.
Set-Alias Cmdlet Microsoft.PowerShell.U... Creates or changes an alias for a cmdlet or other command element in the current Windows PowerShell session.
Set-Variable Cmdlet Microsoft.PowerShell.U... Sets the value of a variable. Creates the variable if one with the requested name does not exist.
Unregister-Event Cmdlet Microsoft.PowerShell.U... Cancels an event subscription.
Update-TypeData Cmdlet Microsoft.PowerShell.U... Updates the extended type data in the session.
Add-Computer Cmdlet Microsoft.PowerShell.M... Add the local computer to a domain or workgroup.
Clear-Content Cmdlet Microsoft.PowerShell.M... Deletes the contents of an item, but does not delete the item.
Clear-Item Cmdlet Microsoft.PowerShell.M... Clears the contents of an item, but does not delete the item.
Clear-ItemProperty Cmdlet Microsoft.PowerShell.M... Clears the value of a property but does not delete the property.
Copy-Item Cmdlet Microsoft.PowerShell.M... Copies an item from one location to another.
Get-PSDrive Cmdlet Microsoft.PowerShell.M... Gets drives in the current session.
Move-Item Cmdlet Microsoft.PowerShell.M... Moves an item from one location to another.
Move-ItemProperty Cmdlet Microsoft.PowerShell.M... Moves a property from one location to another.
New-PSDrive Cmdlet Microsoft.PowerShell.M... Creates temporary and persistent mapped network drives.
New-Service Cmdlet Microsoft.PowerShell.M... Creates a new Windows service.
Register-WmiEvent Cmdlet Microsoft.PowerShell.M... Subscribes to a Windows Management Instrumentation (WMI) event.
Remove-EventLog Cmdlet Microsoft.PowerShell.M... Deletes an event log or unregisters an event source.
Remove-Item Cmdlet Microsoft.PowerShell.M... Deletes the specified items.
Remove-ItemProperty Cmdlet Microsoft.PowerShell.M... Deletes the property and its value from an item.
Remove-PSDrive Cmdlet Microsoft.PowerShell.M... Deletes temporary Windows PowerShell drives and disconnects mapped network drives.
Remove-WmiObject Cmdlet Microsoft.PowerShell.M... Deletes an instance of an existing Windows Management Instrumentation (WMI) class.
New-IseSnippet Function ISE Creates a Windows PowerShell ISE code snippet.
Export-Counter Cmdlet Microsoft.PowerShell.D... The Export-Counter cmdlet takes PerformanceCounterSampleSet objects and exports them as counter log files.
Export-ODataEndpointProxy Function Microsoft.PowerShell.O... Generates a module that contains cmdlets to manage an OData endpoint.
Get-Credential Cmdlet Microsoft.PowerShell.S... Gets a credential object based on a user name and password.
Set-ExecutionPolicy Cmdlet Microsoft.PowerShell.S... Changes the user preference for the Windows PowerShell execution policy.
ConvertTo-SecureString Cmdlet Microsoft.PowerShell.S... Converts encrypted standard strings to secure strings. It can also convert plain text to secure strings. It is used ...
Remove-WSManInstance Cmdlet Microsoft.WSMan.Manage... Deletes a management resource instance.
Disconnect-WSMan Cmdlet Microsoft.WSMan.Manage... Disconnects the client from the WinRM service on a remote computer.
Registry Provider Microsoft.PowerShell.Core Provides access to the system registry keys and values from Windows PowerShell.
Alias Provider Microsoft.PowerShell.Core Provides access to the Windows PowerShell aliases and the values that they represent.
Environment Provider Microsoft.PowerShell.Core Provides access to the Windows environment variables.
FileSystem Provider Microsoft.PowerShell.Core Provides access to files and directories.
Function Provider Microsoft.PowerShell.Core Provides access to the functions defined in Windows PowerShell.
Variable Provider Microsoft.PowerShell.Core Provides access to the Windows PowerShell variables and to their values.
about_Arrays HelpFile Describes arrays, which are data structures designed to store
about_Assignment_Operators HelpFile Describes how to use operators to assign values to variables.
about_Automatic_Variables HelpFile Describes variables that store state information for Windows PowerShell.
about_Checkpoint-Workflow HelpFile Describes the Checkpoint-Workflow activity, which
about_Debuggers HelpFile Describes the Windows PowerShell debugger.
about_Execution_Policies HelpFile Describes the Windows PowerShell execution policies and explains
about_Format.ps1xml HelpFile The Format.ps1xml files in Windows PowerShell define the default display
about_History HelpFile Describes how to get and run commands in the command history.
about_Jobs HelpFile Provides information about how Windows PowerShell background jobs run a
about_Job_Details HelpFile Provides details about background jobs on local and remote computers.
about_Line_Editing HelpFile Describes how to edit commands at the Windows PowerShell command prompt.
about_Modules HelpFile Explains how to install, import, and use Windows PowerShell modules.
about_Preference_Variables HelpFile Variables that customize the behavior of Windows PowerShell
about_Profiles HelpFile Describes how to create and use a Windows PowerShell profile.
about_PSReadline HelpFile
about_PSSessions HelpFile Describes Windows PowerShell sessions (PSSessions) and explains how to
about_PSSession_Details HelpFile Provides detailed information about Windows PowerShell sessions and the
about_Remote_Disconnected_Sess... HelpFile Explains how to disconnect from and reconnect to a PSSession
about_Remote_Requirements HelpFile Describes the system requirements and configuration requirements for
about_Remote_Troubleshooting HelpFile Describes how to troubleshoot remote operations in Windows PowerShell.
about_Scheduled_Jobs HelpFile Describes scheduled jobs and explains how to use and manage
about_Scheduled_Jobs_Advanced HelpFile Explains advanced scheduled job topics, including the file structure
about_Scheduled_Jobs_Troublesh... HelpFile Explains how to resolve problems with scheduled jobs
about_Session_Configurations HelpFile Describes session configurations, which determine the users who can
about_Special_Characters HelpFile Describes the special characters that you can use to control how
about_Types.ps1xml HelpFile Explains how to use Types.ps1xml files to extend the types of objects
about_Variables HelpFile Describes how variables store values that can be used in Windows
about_WMI_Cmdlets HelpFile Provides background information about Windows Management Instrumentation
about_WorkflowCommonParameters HelpFile This topic describes the parameters that are valid on all Windows
about_WS-Management_Cmdlets HelpFile Provides an overview of Web Services for Management (WS-Management) as
about_Scheduled_Jobs HelpFile Describes scheduled jobs and explains how to use and manage
about_Scheduled_Jobs_Advanced HelpFile Explains advanced scheduled job topics, including the file structure
about_Scheduled_Jobs_Troublesh... HelpFile Explains how to resolve problems with scheduled jobs
about_Checkpoint-Workflow HelpFile Describes the Checkpoint-Workflow activity, which
about_WorkflowCommonParameters HelpFile This topic describes the parameters that are valid on all Windows
If we type Get-Help *Delete*, because we know we want to delete items PowerShell provides us with a list of cmdlets that contain the keyword delete either in the cmdlet itself or in the description. Let's review the results to see if we can find a cmdlet that may work for our particular situation. (go ahead take a few minutes to see which one you would use before proceeding to the answer).

Did you look?
Did you find one that may work?
Did you just skip and look ahead?

If you just skipped, shame (insert slow head shake).

The only way you will learn is to do.

Anyway, if we review each cmdlet and description we come upon the Remove-Item cmdlet.

We strongly consider this cmdlet because of the discription, which reads Deletes the specified items.

How can we use the Remove-Item cmdlet? Well, let see.

# If we type the following we will get the documentation of the Remove-Item cmdlet
PS> Get-Help Remove-Item
NAME
Remove-Item

SYNOPSIS
Deletes files and folders.


SYNTAX
Remove-Item [-Confirm] [-Credential ] [-Exclude ] [-Filter ] [-Force] [-Include ] -LiteralPath [-Recurse] [-Stream ]
[-UseTransaction] [-WhatIf] []

Remove-Item [-Path] [-Confirm] [-Credential ] [-Exclude ] [-Filter ] [-Force] [-Include ] [-Recurse] [-Stream ]
[-UseTransaction] [-WhatIf] []

Remove-Item [-Stream ] []


DESCRIPTION
The Remove-Item cmdlet deletes one or more items. Because it is supported by many providers, it can delete many different types of items, including files, folders, registry keys,
variables, aliases, and functions.
In file system drives, the Remove-Item cmdlet deletes files and folders.

If you use the Stream dynamic parameter, it deletes the specified alternate data stream, but does not delete the file.

Note: This custom cmdlet help file explains how the Remove-Item cmdlet works in a file system drive. For information about the Remove-Item cmdlet in all drives, type "Get-Help
Remove-Item -Path $null" or see Remove-Item at http://go.microsoft.com/fwlink/?LinkID=113373.


RELATED LINKS
FileSystem Provider
Clear-Content
Get-Content
Get-ChildItem
Get-Content
Get-Item
Remove-Item
Set-Content
Test-Path

REMARKS
To see the examples, type: "get-help Remove-Item -examples".
For more information, type: "get-help Remove-Item -detailed".
For technical information, type: "get-help Remove-Item -full".
For online help, type: "get-help Remove-Item -online"

If you look at the REMARKS section you can see it tells us how to access some examples of the cmdlet, so lets view these examples.

PS > Get-Help Remove-Item -Examples

NAME
Remove-Item

SYNOPSIS
Deletes files and folders.


-------------------------- EXAMPLE 1 --------------------------

C:\PS>Get-Item C:\Test\Copy-Script.ps1 -Stream Zone.Identifier


FileName: \\C:\Test\Copy-Script.ps1

Stream Length
------ ------
Zone.Identifier 26


C:\PS>Remove-Item C:\Test\Copy-Script.ps1 -Stream Zone.Identifier

C:\PS>Get-Item C:\Test\Copy-Script.ps1 -Stream Zone.Identifier

get-item : Could not open alternate data stream 'Zone.Identifier' of file 'C:\Test\Copy-Script.ps1'.
At line:1 char:1
+ get-item 'C:\Test\Copy-Script.ps1' -Stream Zone.Identifier
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Test\Copy-Script.ps1:String) [Get-Item], FileNotFoundE
xception
+ FullyQualifiedErrorId : AlternateDataStreamNotFound,Microsoft.PowerShell.Commands.GetItemCommand


C:\PS>Get-Item C:\Test\Copy-Script.ps1


Directory: C:\Test


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 8/4/2011 11:15 AM 9436 Copy-Script.ps1

Description

-----------

This example shows how to use the Stream dynamic parameter of the Remove-Item cmdlet to delete an alternate data stream. The stream parameter is introduced in Windows PowerShell 3.0.

The first command uses the Stream dynamic parameter of the Get-Item cmdlet to get the Zone.Identifier stream of the Copy-Script.ps1 file.

The second command uses the Stream dynamic parameter of the Remove-Item cmdlet to remove the Zone.Identifier stream of the file.

The third command uses the Stream dynamic parameter of the Get-Item cmdlet to verify that the Zone.Identifier stream is deleted.

The fourth command Get-Item cmdlet without the Stream parameter to verify that the file is not deleted.




-------------------------- EXAMPLE 2 --------------------------

C:\PS>Remove-Item C:\Test\*.*

Description

-----------

This command deletes all of the files with names that include a dot (.) from the C:\Test directory. Because the command specifies a dot, the command does not delete directories or
files with no file name extension.




-------------------------- EXAMPLE 3 --------------------------

C:\PS>Remove-Item * -Include *.doc -Exclude *1*

Description

-----------

This command deletes from the current directory all files with a .doc file name extension and a name that does not include "1". It uses the wildcard character (*) to specify the
contents of the current directory. It uses the Include and Exclude parameters to specify the files to delete.




-------------------------- EXAMPLE 4 --------------------------

C:\PS>Remove-Item -Path C:\Test\hidden-RO-file.txt -Force

Description

-----------

This command deletes a file that is both hidden and read-only. It uses the Path parameter to specify the file. It uses the Force parameter to give permission to delete it. Without
Force, you cannot delete read-only or hidden files.




-------------------------- EXAMPLE 5 --------------------------

C:\PS>Get-ChildItem * -Include *.csv -Recurse | Remove-Item

Description

-----------

This command deletes all of the CSV files in the current directory and all subdirectories recursively.

Because the Recurse parameter in this cmdlet is faulty, the command uses the Get-Childitem cmdlet to get the desired files, and it uses the pipeline operator to pass them to the
Remove-Item cmdlet.

In the Get-ChildItem command, the Path parameter has a value of *, which represents the contents of the current directory. It uses the Include parameter to specify the CSV file type,
and it uses the Recurse parameter to make the retrieval recursive.

If you try to specify the file type in the path, such as "-path *.csv", the cmdlet interprets the subject of the search to be a file that has no child items, and Recurse fails.

As you can see that is a helpful bit of information.

Review the examples of how to use the cmdlet. Does any seem to fit our particular task?

How about Example 5?

Given a specific directory and a specific list of extensions to delete from the directory we could simply CD into the specified directory and remove the files with the specified extensions.

PS > CD Sample
PS Sample> Get-ChildItem * -Include *.txt, *.docx | Remove-Item

The above snippet would delete files within the Sample directory that had the .txt and .docx extensions.

Beginning to see the usefulness of the Get-Help cmdlet? 🙂

The other two cmdlets Get-Command and Get-Member, I do not use as often. However, you should be familiar with the two. If you put PowerShell on your resume more then likely you will be asked about these two cmdlets, as well as, the Get-Help.



comments powered by Disqus