Basic Usage

First, we will look at commands that are very useful and frequently used in daily usage.

File and Directory

While dealing with files and directories, it should be known that a single dot . represents the current directory, and double dots .. represent the parent directory.

Get-ChildItem (ls)

A cmdlet that lists the content of a specified directory.

PS C:\Users\user> Get-ChildItem    
    Directory: C:\Users\user
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-r---         3/15/2024  11:30 PM                3D Objects
d-r---         3/15/2024  11:30 PM                Contacts
d-r---          4/1/2009  12:10 PM                Desktop
d-r---         3/18/2024  10:39 PM                Documents
d-r---         3/16/2024   9:55 AM                Downloads
d-r---         3/15/2024  11:30 PM                Favorites
d-r---         3/15/2024  11:30 PM                Links
d-r---         3/15/2024  11:30 PM                Music
d-r---         3/15/2024  11:30 PM                Pictures
d-r---         3/15/2024  11:30 PM                Saved Games
d-r---         3/15/2024  11:31 PM                Searches
d-r---         3/15/2024  11:30 PM                Videos

Set-Location (cd)

A cmdlet that changes the working directory.

We saw examples of this earlier.

New-Item

This cmdlet creates a new file or directory.

If no parameter is provided, it creates an empty file by default.

To create a directory, we can provide the -ItemType Directory parameter.

For more examples and advanced usage, you can refer to the help page.

Remove-Item (rm)

This command deletes files or directories.

Copy-Item (cp)

A cmdlet used to copy files or directories.

Move-Item (mv)

Moves or renames files or directories. If only the directory name is given, it will just move the item.

If a new name is provided, it will move and rename the item.

Get-Content (cat)

Used to display the content of files.

System Processes

Get-Process

Displays a list of processes running on the system.

It is often used with filtering options.

If called without parameters, it shows all processes.

Stop-Process:

Terminates a process. It can be called by name or process ID.

Get-Service

Used to display the list of services on the system.

Start-Service

Starts a service.

Stop-Service

Stops a service.

Object Selection and Filtering

In the cmdlets we discussed earlier, we observed commands that produce very long outputs, outputs that we may want to use differently, or outputs from which we may want to access only a single column. We will learn how to manage these.

In PowerShell, piping allows you to chain tasks together powerfully by sending command outputs to the next command. It is represented by the pipe symbol |.

Piping allows you to run multiple commands on a single command line. The output of the previous command becomes the input for the next command. This enables you to break down complex tasks into smaller, more manageable steps and process the outputs according to your needs.

For example, you might want to get a list of running processes and only see their names and IDs.

Select-Object (select)

With this cmdlet, you can choose specific properties of objects in a collection, displaying only the information you need.

In the example above, we only took the process name and ID, ignoring the other non-essential parts.

Where-Object (where)

Allows you to filter objects based on specific criteria. This way, you only process the objects you need.

For example, to list all services and display only the running ones:

Here, the -eq operator stands for equality.

Commonly used operators include:

  • -eq: Equals

  • -ne: Not equal

  • -gt: Greater than

  • -ge: Greater than or equal

  • -lt: Less than

  • -le: Less than or equal

You can find all other operators in the help page of the Where-Object command.

Select-String

The Select-String command is a PowerShell cmdlet used to search and select text lines in text files or strings. You can select lines that match a specific pattern or those that do not.

  • Searching Text Files: It can be used to search for a specific word, phrase, or regex pattern in a text file.

  • String Processing: It can be used to select or replace specific text in a string.

  • Filtering: It can be used to select text lines that match specific criteria.

To search for a specific word in a text file:

Last updated