In PowerShell, $_ is a special variable, often referred to as the current object or pipeline variable. It represents the object that is currently being processed in the pipeline. The $_ variable is used in cmdlets or scripts to reference the current object when performing operations or accessing its properties.

$_ is especially useful in cmdlets where you need to perform actions on each object in a collection, such as when using ForEach-Object, Where-Object, or Select-Object. When combined with .Property, it allows you to access specific properties of the current object.

How $_ Works with .Property

The notation $_ is commonly used in the pipeline to represent the current object, and the .Property allows you to access one of its properties. You would use $_ in conjunction with the dot operator to reference the properties or methods of the object as it passes through the pipeline.

For example, when working with an array of objects that each have properties, you can use $_ to refer to each object in turn, and then use .Property to access or manipulate the desired property.

Example:

Get-Process | ForEach-Object { $_.Name }

In this example:

  • Get-Process retrieves a list of processes running on the system.
  • ForEach-Object iterates over each process.
  • $_ represents the current process object in each iteration, and .Name accesses the name of the process.

Practical Usage of $_ and .Property

1. Filtering Objects with Where-Object

You can use $_ to filter objects based on their properties. The Where-Object cmdlet filters items from the pipeline that meet specific conditions.

Example:

Get-Process | Where-Object { $_.CPU -gt 100 }

This command:

  • Uses Get-Process to retrieve a list of processes.
  • The Where-Object cmdlet filters the processes where the CPU property is greater than 100.

2. Accessing Specific Properties in Collections

When working with collections, you may want to select or output specific properties of each item in the collection.

Example:

Get-Service | ForEach-Object { $_.DisplayName }

This command:

  • Retrieves a list of services using Get-Service.
  • Iterates through each service with ForEach-Object.
  • Outputs only the DisplayName property of each service.

3. Modifying Properties in Objects

You can use $_ to access and modify properties of objects as they pass through the pipeline.

Example:

Get-Process | ForEach-Object { $_.PriorityClass = 'High' }

This command:

  • Retrieves a list of processes.
  • Iterates over each process with ForEach-Object.
  • Changes the PriorityClass property of each process to “High”.

4. Using .Property to Access Nested Properties

Some objects have nested properties or collections of properties. You can use $_ to access these nested properties.

Example:

Get-ADUser -Filter * | ForEach-Object { $_.Address.Country }

In this example:

  • Retrieves all Active Directory users using Get-ADUser.
  • Iterates over each user and accesses the nested Country property inside the Address property.

Common Cmdlets Using $_ and .Property

1. Select-Object: Select specific properties of objects.

Get-Process | Select-Object Name, CPU

2. Where-Object: Filter objects based on a condition.

Get-Process | Where-Object { $_.Id -eq 1234 }

3. ForEach-Object: Perform actions on each object in the pipeline.

Get-Service | ForEach-Object { $_.Status }

4. Sort-Object: Sort objects based on specific properties.

Get-Process | Sort-Object {$_.CPU}