Learn-Software "PowerShell" -Basic

Learn the basic operations of the shell (command-line interface).

Page content

Introduction

Learn how to control a computer using PowerShell (a command-line interface) This article covers how to type the command in PowerShell, tips, and the commands that I use frequently.

All “shell” operations are performed by typing text called “the command.” Although the operation may be difficult to grasp at first, you will become proficient at it through trial and error. The command operations are the same as those used in daily GUI operations, such as creating, moving, copying, deleting, renaming, starting, and listing files and directories (folders).

Prerequisites

Type the Command

Basic Shell Operations

The basic steps to operate the computer by typing commands into the shell are as follows:

  1. Open the shell: See → Launch-PowerShell
  2. Type the command: For example, type ls
  3. Execute the command: After typing the command, press ENTER
  4. Check the command output:
    • The command execution result is output to the terminal
    • If an error occurs, an error message is output

Example: Execute the command “Get-Date ” to get the current date and time.

Get-Date
Sunday, February 16, 2025 4:11:50 PM

Example: Read the help for the PowerShell command “Get-Content ” in a browser.

Get-Help Get-Content -Online

Use Auto-Completion to type the command easier and faster

Although PowerShell command naming conventions are consistently structured as <verb>-<noun> and intuitively easy to understand, they are long and tedious to type manually. Actively utilize Autocompletion to make command type easier and more accurate.

  1. Command History
    • You can reuse previously typed commands by pressing the and keys
    • You can get a list of history with the “Get-History ” command
      • See → about_History
      • Get the path of the history file: (Get-PSReadlineOption).HistorySavePath
  2. TAB-Completion
    • Type part of the command (or filename) and press TAB
    • If there are multiple candidates, pressing TABTAB … cycles through the candidates
    • Example: Type Get-ContTAB → completed to Get-Content
  3. CTRL+SPACE Completion (MenuComplete )
    • Type part of the command (or filename) and press CTRL+SPACE
    • Candidates are listed, so use the arrow keys to select and press ENTER
    • Example: Type Get-ChCTRL+SPACE → candidate list is displayed
  4. PSReadLine Predictive IntelliSense (IntelliSense )
    • Overview: As you type part of the command (or filename), prediction candidates based on command history, etc. are displayed
    • Operation:
      • InlineView (Default): accept with and execute with ENTER
      • ListView (Switch with F2): select with , and execute with ENTER
    • Check the settings:
      • Get-PSReadLineOption | Select-Object -Property PredictionSource
      • If it’s other than None (for example, History), it’s enabled

Use Short Names (Aliases) for Easier command typing

Commands with registered short names (aliases ) can be typed with shorter alternate names.

  • How to use aliases in PowerShell
    • Search aliases: Use the “Get-Alias ” command to check the registered aliases
    • Use aliases: Type the alias instead of the command fullname
      • Example: ls is an alias for Get-ChildItem

The following two commands are equivalent (man is an alias for Get-Help, cat is an alias for Get-Content):

Get-Help Get-Content
# use aliases
man cat

Get and read Help

You can learn about the command summary and usage with the “Get-Help ” command. PowerShell commands have comprehensive help, so just reading them is a good way to learn PowerShell.

  • How to use “Get-Help ”:
    • Get-Help <Name>
    • Get-Help <Name> -Online
      • The -Online option displays the information on the web in a browser
Get-Help Get-Content
NAME
    Get-Content

SYNTAX
    Get-Content [-Path] <string[]>...
    Get-Content -LiteralPath <string[]>...

ALIASES
    gc
    cat
    type

REMARKS
    Get-Help cannot find the Help files for this cmdlet on this computer.
     It is displaying only partial help.
        -- To download and install Help files for the module that includes
    this cmdlet, use Update-Help.
        -- To view the Help topic for this cmdlet online, type: "Get-Help
     Get-Content -Online" or
           go to https://go.microsoft.com/fwlink/?LinkID=2096490.

Searching the Command

You can search for commands with “Get-Command .”

  • How to use “Get-Command ”:
    • Get-Command *: Get all types of commands
    • Get-Command Get-*: Get commands that include Get-
    • Get-Command *-Content: Get commands that include -Content

Knowing that the PowerShell command naming conventions are structured as “<verb>-<noun>” allows you to go some way to predicting whether commands exist, even if you don’t know them. You can also specify and search explicitly with -Noun or -Verb options.

# Search for commands that include "Content" in <noun>
Get-Command -Noun "Content"
CommandType     Name            Version
-----------     ----            -------
Cmdlet          Add-Content     7.0.0…
Cmdlet          Clear-Content   7.0.0…
Cmdlet          Get-Content     7.0.0…
Cmdlet          Set-Content     7.0.0…

Daily Command Operations

For me, daily computer operations involve working with the filesystem (files and directories). The following shows the workflow with the corresponding commands. The same operations can be done visually using Explorer and mouse clicks, without using the CLI (command line interface).

Operation Command (Alias)
1. Move directories cd , pushd , popd
2. List files in a directory ls
3. Open, create, move, copy, delete, rename files ii , mkdir , ni , mv , cp , rm , ren
4. Edit, save, and close files through the appropriate application

Examples of actual operations. Using aliases, TAB completion, and command history to reduce input work:

Operation Command
1. List files in a directory
( → As a result, the file we were looking for was not found. 😢)
ls path/to/the/directory
2. List files in another directory
( → As a result, we find the file we want. 🙄)
ls path/to/the/another/directory
3. Move to the directory pushd path/to/the/another/directory
4. Open the file ii file.xlsx
5. Edit, save, and close the file through the appropriate application
6. Return to the previous directory popd

Each command is explained in detail below. For more detailed information, run man <command> -Online. The ii command (an alias for Invoke-Item ) is particularly useful. When you run ii <file>, the file will be opened by the appropriate application based on its extension. Once you are familiar with the ii  command, you will be able to operate frequently used folders and files without having to move from the current directory:

Operation Command
1. Open the file (or directory) ii path/to/file.xlsx
2. Edit, save, and close the file through the appropriate application

ls (Get-ChildItem )

Synopsis: Lists the files and folders in the current directory.

Param Command Note
Help man ls -Online
Example ls Lists file and folder information in the current directory.
Example ls -Name Displays only file and folder names in the current directory.
Example ls *.txt Lists files named *.txt.
Example ls *.txt -Recurse Lists files named *.txt and recursively searches subdirectories.
Example Get-ChildItem | Select-Object -ExpandProperty FullName Gets the full path of files and directories in the current directory as strings.

Back : Daily Command Operations Table

cd (Set-Location )

Synopsis: Moves to the specified directory.

Param Command Note
Help man cd -Online
Example cd, cd ~ Moves to the home directory.
Example cd C:/Users/btklab/ Absolute path: Moves to the home directory of user btklab.
Example cd ./ Relative path: Stays in the current directory (does not move).
Example cd ../ Relative path: Moves to the parent directory.
Example cd - Moves back to the previous directory.

Back : Daily Command Operations Table

pushd (Push-Location )

Synopsis: Adds the current location to the location stack and moves to the specified directory. By using pushd followed by popd , you can return to the current location (the location registered at the top of the stack).

Param Command Note
Help man pushd -Online
Example cd ~ ; pushd C:/Users/ Absolute path: Moves from the home directory to the Users directory.
Example popd Moves back to the home directory (the location before the Users directory).

Back : Daily Command Operations Table

popd (Pop-Location )

Synopsis: Returns to the last location pushed onto the stack by pushd .

Param Command Note
Help man popd -Online
Example cd ~ ; pushd C:/Users/ Absolute path: Moves from the home directory to the Users directory.
Example popd Moves back to the home directory (the location before the Users directory).

Back : Daily Command Operations Table

ii (Invoke-Item )

Synopsis: Opens files or folders with the associated application.

Param Command Note
Help man ii -Online
Example ii . Opens the current directory in the explorer.
Example ii a.txt Opens the file a.txt in the text editor.
Example ii a.xlsx Opens the file a.xlsx in Excel.
Tips
There is a similar command called [Start-Process][]. Both open a.txt in a text editor:
Invoke-Item "C:/path/to/file.txt"
Start-Process "notepad.exe" "C:/path/to/file.txt"
Use Invoke-Item to easily open files or folders.
Use Start-Process when you want to launch a specific application with specific arguments.

Back : Daily Command Operations Table

mkdir (New-Item )

Synopsis: Creates a new directory (folder).

Param Command Note
Help man mkdir -Online
Example mkdir "newdir" Creates a new directory newdir in the current directory.
Example New-Item -Path . -Name "newdir" -ItemType "directory" Same as above.

Back : Daily Command Operations Table

ni (New-Item )

Synopsis: Creates a new directory or file.

Param Command Note
Help man ni -Online
Example ni -Path . -Name "a.txt" -ItemType "file" Creates an empty file a.txt in the current directory.
Example New-Item -Path . -Name "a.txt" -ItemType "file" Same as above.

Back : Daily Command Operations Table

mv (Move-Item )

Synopsis: Moves or renames a file or folder. Adding the -WhatIf switch allows you to confirm the operation without actually executing it.

Param Command Note
Help man mv -Online
Example mv C:/a.txt E:/Temp/b.txt Renames the file a.txt and moves it from the C: drive to the E:/Temp directory, changing its name to b.txt.
Example mv -Path C:/a.txt -Destination E:/Temp/b.txt Same as above.
Example ls a.txt | mv -Destination E:/Temp/ Moves the file a.txt from the current directory to the E:/Temp directory. In this case, explicitly specify the -Destination option.

cp (Copy-Item )

Synopsis: Copies files or folders. The original file is not deleted. By adding the -WhatIf switch, you can verify the action without executing it.

Param Command Note
Help man cp -Online
Example cp ./a.txt ../hoge File copy: Copies the file a.txt from the current directory to the directory hoge one level up.
Example cp -Path ./a.txt -Destination ../hoge Same as above.
Example cp -Path "C:/hoge" -Destination "C:/fuga" -Recurse Directory copy: Copies the directory C:/hoge and its contents to the directory C:/fuga.
Example ls a.txt | cp -Destination E:/Temp/ Copies the file a.txt from the current directory to the E:/Temp directory. In this case, explicitly use the -Destination option. An error is returned if the item does not exist.

Back : Table of everyday command operations

rm (Remove-Item )

Synopsis: Deletes files or directories. If the specified item does not exist, it returns an error and does not execute. Note that deleted items are not moved to the “Recycle Bin” and cannot be restored. By adding the -WhatIf switch, you can verify the action without executing it.

Param Command Note
Help man rm -Online
Example rm a.txt Deletes the file a.txt from the current directory.
Example ls a.txt | rm Same as above.
Example Remove-Item -Path a.txt Same as above.
Example rm *.txt Deletes one or more files with the .txt extension from the current directory.
Example ls *.cache -Recurse | rm Deletes one or more files with the .cache extension from the current directory and subdirectories.
Example Remove-Item a.md -WhatIf Dry run: The result is output to the console without actually executing it.

Back : Table of everyday command operations

ren (Rename-Item )

Synopsis: Renames files or folders. By adding the -WhatIf switch, you can verify the action without executing it.

Param Command Note
Help man ren -Online
Example ren "./a.txt" "b.txt" File rename: Renames the file a.txt in the current directory to b.txt.
Example ren -Path "./a.txt" -NewName "b.txt" Same as above.
Example Rename-Item -Path "./a.txt" -NewName "b.txt" Same as above.
Example Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace ‘.txt’,’.log’ } Batch rename of multiple files: Renames the extension of files with the .txt extension in the current directory to .log.

Back : Table of everyday command operations

Terms

PowerShell Commands

In the world of PowerShell, “the command” are referred to as “the cmdlet.” Knowing that the cmdlet follow the naming convention of <Verb>-<Noun> can help you (to some extent) predict the existence of unknown commands.