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.
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.
PS C:\Users\user> Get-Service
Status Name DisplayName
------ ---- -----------
Stopped AarSvc_40ce5 Agent Activation Runtime_40ce5
Stopped AJRouter AllJoyn Router Service
Stopped ALG Application Layer Gateway Service
Stopped AppIDSvc Application Identity
Running Appinfo Application Information
Stopped AppMgmt Application Management
Stopped AppReadiness App Readiness
Stopped AppVClient Microsoft App-V Client
Running AppXSvc AppX Deployment Service (AppXSVC)
Stopped AssignedAccessM... AssignedAccessManager Service
Running AudioEndpointBu... Windows Audio Endpoint Builder
Running Audiosrv Windows Audio
Stopped autotimesvc Cellular Time
Stopped AxInstSV ActiveX Installer (AxInstSV)
Running BalloonService BalloonService
Stopped BcastDVRUserSer... GameDVR and Broadcast User Service_...
Stopped BDESVC BitLocker Drive Encryption Service
Running BFE Base Filtering Engine
Stopped BITS Background Intelligent Transfer Ser...
Stopped BluetoothUserSe... Bluetooth User Support Service_40ce5
Running BrokerInfrastru... Background Tasks Infrastructure Ser...
Stopped BTAGService Bluetooth Audio Gateway Service
...
PS C:\Users\user> Start-Service -Name Appinfo
PS C:\Users\user> Stop-Service -Name Appinfo
Get-Process | Select-Object ProcessName, Id
Get-Service | Where-Object Status -eq "Running"
PS C:\Users\user\Documents> Select-String -Pattern "today" .\file.txt
file.txt:1:The purpose of today's training is to defeat yesterday's understanding. - Miyamoto Musashi