Pages

Monday, September 23, 2013

Demystifying the Command Line

The command line can be a confusing place. There is lots of black space and no where to click. This post is my attempt to at least partially demystify the command line. I realize this can be an intimidating tool. Powerful tools will always seem more difficult when you first adopt them.
Let's start with a simple PowerShell example to create a user account.
New-ADUser clark.kent -surname "Kent" -givenname "Clark" -displayname "Clark Kent"
I want to break this command into components. The first component is "New-ADUser". This is the command to be executed. In PowerShell, this is referred to as a PowerShell Cmdlet (pronounced command-let). This is the program (or script) to be executed. A quick note about case. Windows is case-insensitive which means you can use new-aduser, NEW-ADUSER, or nEW-aduSER. All of these examples are valid.
The next component is "clark.kent". This is an argument or parameter. Arguments are either optional or required. A command may have no arguments, one argument, or multiple arguments. Whether a argument is required and the number of arguments to use is determined by the command (or cmdlet).
The final component in this example is an option. The options in the above example are "-surname", "-givenname", and "-displayname". An option is just what the name suggests, a way to provide additional information at the command line. Command line options are typically not required. Some options will require a corresponding value while others will work as a toggle (to enable or disable a certain behavior). The options above all have corresponding values. See the table below.
OptionValue
surnameKent
givennameClark
displaynameClark Kent
A side note about options. The specifier for options will vary based on the command, operating system, or environment. In PowerShell, the specifier is the dash symbol (-). In the Windows command line, it is typically the slash (/). In Linux, the dash or double dash is common (- or --).
When you are using PowerShell, you can always find help with the Get-Help cmdlet. To find help with the New-ADUser cmdlet, use "Get-Help New-ADUser". You may be able to find examples by using the -examples option (i.e. "Get-Help New-ADUser -examples").
Finally, consider the example below. Hopefully the information above allows you to better understand this more complex command.
New-ADUser –Name "Jimmy Olsen" –SamAccountName jimmy.olsen –DisplayName "Jimmy Olsen" –Title "Photojournalist" –Enabled $true –ChangePasswordAtLogon $true -AccountPassword (ConvertTo-SecureString "TheD@ilyPlan3t" -AsPlainText -force) -PassThru
Try to identify the components of this command.
This example uses PowerShell but the concepts apply to other systems. I hope this helps you to understand the command line a little better.