Bird
0
0

Which command achieves this?

hard📝 Application Q15 of 15
PowerShell - Cmdlets and Pipeline
You want to create a list of running processes showing only their 'Name' and 'Id', but also add a new property 'ShortName' that shows the first 3 letters of the process name. Which command achieves this?
AGet-Process | Select-Object -Property Name, Id, ShortName
BGet-Process | Select-Object Name, Id, ShortName = $_.Name.Substring(0,3)
CGet-Process | Select-Object Name, Id, @{Name='ShortName';Expression={$_.Name.Substring(0,3)}}
DGet-Process | Select-Object Name, Id | Add-Member -Name ShortName -Value {$_.Name.Substring(0,3)}
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to add calculated properties

    Use a hashtable with Name and Expression keys inside Select-Object to add new properties.
  2. Step 2: Check each option

    Get-Process | Select-Object Name, Id, @{Name='ShortName';Expression={$_.Name.Substring(0,3)}} correctly uses @{Name='ShortName';Expression={$_.Name.Substring(0,3)}} to add the new property. Get-Process | Select-Object Name, Id, ShortName = $_.Name.Substring(0,3) has wrong syntax. Get-Process | Select-Object -Property Name, Id, ShortName tries to select a non-existent property. Get-Process | Select-Object Name, Id | Add-Member -Name ShortName -Value {$_.Name.Substring(0,3)} uses Add-Member incorrectly for pipeline objects.
  3. Final Answer:

    Get-Process | Select-Object Name, Id, @{Name='ShortName';Expression={$_.Name.Substring(0,3)}} -> Option C
  4. Quick Check:

    Use hashtable with Expression to add calculated property [OK]
Quick Trick: Add calculated properties with @{Name='';Expression={}} in Select-Object [OK]
Common Mistakes:
  • Trying to assign calculated properties directly without hashtable
  • Selecting non-existent properties without calculation
  • Using Add-Member incorrectly in pipeline

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes