PowerShell is replacing CMD in next Windows 10 update

In the next big Windows 10 update (aka Creator’s Build update), PowerShell will officially replace the CMD.exe command prompt. CMD was introduced in MS-DOS back in the 80’s, so it has survived for over 35 years now. However, users can still type “CMD” on the File Explorer bar to launch the new PowerShell.

So, with this in mind, if you haven’t used PowerShell up to now, now it’s a good time to start.

Introduction to PowerShell

PowerShell offers two interfaces. Advanced users can use PowerShell ISE (Integrated Scripting Environment) which includes GUI. All users can use the PowerShell Console, which works as a simple command line interface.

If we enter “powershell” in Search, we will be offered to open particular PoserShell application. We will right click it and choose to run it as administrator.

As administrators, we will typically use PowerShell a lot, so it might be a good idea to pin it to our task bar.

Old-fashioned commands

Old command-line syntax actually works great in PowerShell. For example, with cd we can changes folders, with dir we can list all the files and folders included in the current folder, etc.

Commands like cd and dir aren’t native PowerShell commands. They’re aliases for real PowerShell commands.

Getting help

We can get help by typing help followed by a command we know. For example, help cd.

PowerShell help tells us that cd is an alias for the PowerShell command Set-Location. If  we type this command, we will get the same result as the cd.

Help files for PowerShell aren’t installed automatically. To retrieve them we can type update-help, while in administrator mode. From that point on, we can type get-help followed by the command (“cmdlet” in PowerShell world, pronounced “command-let”). For example, get-help set-location produces a summary of that command. We can also pass options to the get-help command.

get-help set-location -examples

produces detailed examples of how to use set-location. The PowerShell command

get-help set-location -detailed

also includes a detailed explanation of parameters for the set-location cmdlet.

Help on parameters

To get all the details about parameters for the cmdlet, we can use the -full parameter, like this:

get-help set-location -full

That produces a line-by-line listing of what we can do with the cmdlet.

cmdlet names

All cmdlet names follow the same verb-noun convention, verb preceding a (singular) noun. Here are some of the common cmdlets:

  • set-location: Sets the current working location to a specified location
  • get-content: Gets the contents of a file
  • get-item: Gets files and folders
  • copy-item: Copies an item from one location to another
  • remove-item: Deletes files and folders
  • get-process: Gets the processes that are running on a local or remote computer
  • get-service: Gets the services running on a local or remote computer
  • invoke-webrequest: Gets content from a webpage on the internet

To see how a particular cmdlet works, use get-help, as in

get-help remove-item -full

Based on its help description, we can figure out what the cmdlet wants. For example, to delete all file that include a dot (“.”) from the C:\Test directory, we can enter the command:

remove-item C:\Test\*.*

If we mistype a cmdlet and PowerShell, we will get a thorough description of what went wrong.

Some cmdlets help us control PowerShell itself:

  • get-command: Lists all available cmdlets (it’s a long list!)
  • get-verb: Lists all available verbs (the left halves of cmdlets)
  • clear-host: Clears the display in the host program

We can filter available cmdlets. For example, to see a list of all the cmdlets that work with Azure services, try this:

get-command *-Azure*

It lists all the verbs that are available with Azure in name.

We can combine these cmdlets with other cmdlets to dig down into almost any part of PowerShell. That’s where pipes come into the picture.

Redirection and pipes

Both redirection (the > character) and pipes (the | character) take the output from an action and output it somewhere else. For example, we can redirect the output of a dir command to a text file, or “pipe” the result of a get-command command into a select-string command, to filter out interesting results, , and then output the result to temp.txt:

dir > temp.txt

get-command *Azure* | select-string “Repository” > temp.txt

In the second command above, the select-string command looks for the string Repository in the piped output and sticks all the lines that match in a file called temp.txt.

The second command can also be written like this:

get-command *Azure* | select-string "Repository" | out-file temp.txt

Using redirection and pipes greatly expands the Windows command line’s capabilities. PowerShell piping capability isn’t restricted to text. In PowerShell we can pass an entire object from one cmdlet to the next, where an “object” is a combination of data (called properties) and the actions (methods) that can be used on the data. However, the kind of object delivered by one cmdlet has to match up with the kinds of objects accepted by the receiving cmdlet. Text is a very simple kind of object so lining up items is easy. Other objects aren’t so rudimentary.

To cope with that we can use the get-member cmdlet. It helps us to find what type of object a cmdlet produces. For example, to figure out what the get-command cmdlet produces:

get-command | get-member

Running that command produces a long list of properties and methods for get-command, but at the beginning of the list we can see one of the type of objects that get-process creates:

TypeName: System.Management.Automation.AliasInfo

So, if we want to manipulate the output of get-command, we need to find another cmdlet that will work with the mentioned TypeName as input. To find those cmdlets, we can enter:

get-command -Parametertype System.Management.Automation.AliasInfo

That produces a list of all of the cmdlets that can handle System.Management.Automation.AliasInfo.

Some cmdlets like where-object can be used to loop through each item sent down the pipeline, one by one, and apply whatever selection criteria you request. We can use a special marker called $_. to step through each item in the pipe.

Say you wanted to come up with a list of all of the cmdlets called “svchost”—in PowerShell speak, you want to match on a Name property of Out-String. Try this PowerShell command:

get-command | where-object {$_.Name -eq “Out-String”}

The where-object cmdlet looks at each item, compares the .Name of that item to “Out-String”. If the item matches, it is typed on the monitor.

More information

 

We suggest  the course on the Microsoft MSDN site written by PowerShell guru JuanPablo Jofre: Getting Started with Windows PowerShell.

We also suggest a book by Don Jones and Jeffrey Hicks “Learn Windows PowerShell in a Month of Lunches,” third edition, which is due in December 2016.

Leave a Reply

%d bloggers like this: