Scripting

PowerShell

ForEach-Object

ForEach-Object { $_ }

Takes each item on the pipeline and handles it as $_

`[cmdlet]

% { [cmdlet] $_ }`

Get-Content C:\path\to\file.txt | ForEach-Object { $_.ToUpper() }

Converts all text in a file to uppercase.

Get-Content C:\path\to\file.txt | ForEach-Object { $_.Trim() }

Trims whitespace from the start and end of each line in a file.

Processes each line in a file and outputs to a new file. Customize the do command for specific modifications:

for /f "tokens=*" %i in (C:\path\to\file.txt) do @echo %i

Where-Object

Where-Object condition (alias where or ?):

Get-Process | Where-Object {$_.name -eq "notepad"}

Loops

Do Loop

Do-While Loop

$counter = 1
do {
    Write-Host "Do-While Loop Iteration: $counter"
    $counter++
} while ($counter -le 5)

Do-Until Loop

Do-While Loop with Condition at the End

Do-Until Loop with Condition at the End

Do-While Loop with Break

For Loop

Basic For Loop

For Loop with Array

ForEach Loop (For Each Element in an Array)

ForEach Loop (For Each Item in a Directory)

ForEach-Object Loop (Pipeline)

ForEach Loop (Associative Array/HashTable)

Foreach Loop

Example 1: ForEach Loop (For Each Element in an Array)

Example 2: ForEach Loop (For Each Element in a Range)

Example 3: ForEach Loop (For Each Item in a Directory)

Example 4: ForEach Loop (For Each Key-Value Pair in a Hashtable)

Example 5: ForEach-Object Loop (For Each Object in a Pipeline)

While Loop

while (){}

Example 1: Basic While Loop

Example 2: While Loop with User Input

Example 3: Infinite While Loop with Break

Conditions

Example 1: While Loop with a Condition

Example 2: Do-While Loop

Example 3: While Loop with User Input

Example 4: Do-While Loop with User Input

Example 5: While Loop with a Break Condition

Generating Ranges

Echo "Hello!" 10 times.

Generate a range of numbers in reverse order from 10 to 1

Generate a range of letters from 'A' to 'Z'

Generate a range of even numbers from 2 to 10 with a step size of 2

Generate a range of numbers based on variables

Properties

Get-Process | Get-Member

Gives the methods and properties of the object/cmdlet.

(cmdlet).property

Command Structure.

(GetProcess).Name

Returns the single property of 'name' of every process.

-ExpandProperty

Extracts values from properties.

Functions

Get-Help about_Functions

Displays the help about functions

Get-Help about_Functions_Advanced

Displays some more in-depth help about functions

Function Do-Stuff { Get-Date; Get-Process; Get-Service }

Creates a function

Do-Stuff

Runs the function

Comments

Creates a comment beside cmdlet:

Multi-line comment:

How to find the data type

$PSHome | Get-Member

Displays System.String with its objects and properties.

$A=12

Creates variable A with an integer value of 12.

$A | Get-Member

Displays System.Int32 with its objects and properties.

CMD

Delete all .txt files in the current directory older than 7 days:

Delete files older than 30 days in a specific directory

/s: Recurse into subdirectories.

/m *.txt: Include only files with the .txt extension.

/d -7: Include files that are older than 7 days (negative value signifies "older than").

/c "cmd /c del @file": Execute the del @file command for each matching file.

Move all files newer than yesterday from the C:\Temp folder to the D:\Archive folder:

/s: Recurse into subdirectories.

/m *: Include all files (regardless of extension).

/d +1: Include files that are newer than yesterday (positive value signifies "newer than").

/c "cmd /c move @file D:\Archive": Execute the move @file D:\Archive command for each matching file.

Copy all .jpg files from the C:\Photos folder to the D:\Backup folder:

Copy all PDF files from one directory to another

Print the names of all txt files in a directory

Rename all .docx files in the current directory to start with "Document_":

Change the extension of all .txt files to .bak in a directory

Recursively list the paths of all files in a directory and its subdirectories

Log the names and sizes of all .exe files in the current directory and subdirectories:

Execute a PowerShell command on each file in a directory

Print the last modified date of all files in a directory

Create a directory for each file in a directory (using the file name as the directory name)

@file

Represents the full path of the current file being processed by the loop or command.

Its value changes as the loop iterates through a set of files.

Use it within commands to perform actions specific to each file, like copying, deleting, or hashing.

@date

Holds the date and time associated with the current file being processed.

The specific format of @date depends on the command being used.

forfiles typically provides access to date components like creation, modification, or access time.

It allows comparing file dates with specific criteria within the loop.

Last updated

Was this helpful?