PowerShell Basics - SWHS-PC/Users GitHub Wiki
If you follow the instructions in this Wiki, you'll be using the command line to work with GIT. (There are graphical tools for GIT, but it's easier to list a sequence of commands than to try to describe where to click in a GUI that may be different on different platforms or in different versions.)
In case you're not familiar with command-line interfaces, this page gives a primer on a few basic commands and concepts you will need. The instructions here are for PowerShell, which is the default command-line interface in Windows. However, most of these basic commands are very similar in the Mac terminal or Linux command shells. (You can also install the open-source version of PowerShell on the Mac or Linux.)
In this page:
- Starting PowerShell
- Working with file and directory paths
- Changing directories (cd)
- Listing files (ls)
- Other common commands
Starting PowerShell
Following are two ways to start PowerShell. As with any program, you can also pin PowerShell to the task bar or start menu for quick access.
To start PowerShell by opening the start menu and typing:
- Click the Start button or press the Window key on the keyboard.
- Type "powersh". That should be enough for "Windows PowerShell" to show as the best match in the results list.
- Press ENTER.
To start PowerShell using the power-user menu:
- Open the so-called power-user menu by right-clicking the Start button, or by holding down the Windows key and pressing X.
- Choose Windows PowerShell from the menu. (Note: There's an option in Settings that can change this menu to show Command Prompt instead of PowerShell.)
A window will open with a copyright notice followed by a prompt that looks something like this:
PS C:\Users\YourName>
The prompt is followed by a blinking cursor where you can type commands.
Working with file and directory paths
Files on your computer are organized into folders (also called directories), which in turn are organized into a hierarchy of folders and subfolders. A directory path is simply a series of steps that describe a path from one place to another in the folder hierarchy.
For example, consider the path C:\Users\YourName. Read from left to right, this means:
- C:\ means start a the top level (or root) of the C drive.
- Users means go from there to the "Users" folder.
- YourName means go from there to the "YourName" folder.
A file path is exactly the same as a directory path, except that the step in the path happens to be the name of a file instead of a folder.
The above example is an absolute path because it starts at the top level of the hierarchy (i.e., a drive or volume), but it is often more convenient to use relative paths. A relative path gives directions from some starting point (usually the current directory). Relative paths take advantage of two special names:
- "." means the current location
- ".." means the parent of the current location (i.e., up one level)
Following are some examples of relative paths and their meanings, assuming the current directory is C:\Users\YourName.
- ".\Documents" takes you to C:\Users\YourName\Documents.
- ".\Documents\History" takes you to C:\Users\YourName\Documents\History.
- "..\Bob" takes you up one level to C:\Users and then down to C:\Users\Bob.
- ".\Readme.txt" refers to the Readme.txt file in the current directory.
Changing directories (cd)
At any given time, the PowerShell prompt specifies the current directory. When you first start PowerShell, the current directory is your user profile directory, which contains folders associated with your account on the computer. For example, your "Documents" and "Pictures" folders are subdirectories of your user profile directory. You can change the current directory using the "cd" command.
Starting from your user profile directory, you can move to the Documents subfolder by typing "cd .\Documents" and pressing ENTER. From the Documents subfolder, you can go to the profile root again (i.e., up one level) by typing "cd .." and pressing ENTER. Each time you enter a comment, you get a new PowerShell prompt showing the current directory, as shown below:
PS C:\Users\YourName> cd .\Documents\
PS C:\Users\YourName\Documents> cd ..
PS C:\Users\YourName>
PowerShell supports auto-completion as a convenience. Instead of typing "cd .\Documents" you can just type "cd doc" and press TAB. The "doc" is automatically expanded to ".\Documents." If there are more than one folder starting with "doc" then you may have to press TAB more than once to cycle through the matching folders until you get the one you want.
You can get more information about the cd command by typing "get-help cd". When you do this, you'll see that the actual name of the command is Set-Location. The shorter name "cd" can be used as an alias, which is nice because it's quicker to type and is also familiar to those coming from other command-line interfaces (including the older Windows command prompt).
Listing files (ls)
You can get a list of files from subdirectories by using the "ls" command. If you must type "ls" by itself, you'll get a list of all files and subdirectories in the current directory.
You can filter the list by specifying a name, which may include wildcard characters. The '*' wildcard matches any sequence of characters, and the '?' wildcard matches any one character. For example, the following command lists all items in the current directory whose names end in ".md":
PS C:\GitHub\SWHS-PC\Users> ls *.md
Directory: C:\GitHub\SWHS-PC\Users
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 9/30/2017 1:57 PM 227 README.md
You can also specify a path (either absolute or relative), and the path may include wildcards. For example, suppose you are in the SWHS-PC directory and you want to get a listing of files with the ".md" extension in the Users subdirectory. You would use the following command:
PS G:\GitHub\SWHS-PC> ls .\Users\*.md
Directory: G:\GitHub\SWHS-PC\Users
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 9/30/2017 1:57 PM 227 README.md
If you type "get-help ls", you'll find that the ls command is an alias for the actual command name, which is Get-ChildItem.
Other common commands
Following are some other very common commands. To learn more about each of them, type get-help followed by the command line.
- New-Item, alias md (for "make directory")
- Remove-Item, alias rm or rd (for "remove" or "remove directory")
- Copy-Item, alias cp
All this is just scratching the surface, but there are many fine PowerShell tutorials on the web if you want to learn more.