Fixes #13184 - Fix .foreach() example so it works in 5.1 - #13185
Conversation
PoliCheck Scan ReportThe following report lists PoliCheck issues in PR files. Before you merge the PR, you must fix all severity-1 and severity-2 issues. The AI Review Details column lists suggestions for either removing or replacing the terms. If you find a false positive result, mention it in a PR comment and include this text: #policheck-false-positive. This feedback helps reduce false positives in future scans. ✅ No issues foundMore information about PoliCheckInformation: PoliCheck | Severity Guidance | Term |
There was a problem hiding this comment.
Pull request overview
This PR updates the PowerShell 5.1 about_Arrays documentation to correct the .ForEach() example so it works as intended in Windows PowerShell 5.1.
Changes:
- Updates the document metadata date.
- Modifies the
.ForEach()example’s$myObjectinitialization.
Comments suppressed due to low confidence (1)
reference/5.1/Microsoft.PowerShell.Core/About/about_Arrays.md:600
- Changing
$myObjectto a hashtable makes the subsequent.ForEach('singleValue')/.ForEach('arrayValue')examples inconsistent:ForEach(string propertyName)retrieves a property from each item in a collection, but a hashtable enumeratesDictionaryEntryobjects (withKey/Value), notsingleValue/arrayValue. This means the sample still won’t work as written in 5.1. Define$myObjectas a collection (single-item array) containing an object with the expected properties, and also update the laterForEach('Value')/$_.Valuereferences in this example to usearrayValueinstead.
$myObject = @{
singleValue = 'Hello'
arrayValue = @(0..10)
}
</details>
---
💡 <a href="/MicrosoftDocs/PowerShell-Docs/new/main?filename=.github/instructions/*.instructions.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add Copilot custom instructions</a> for smarter, more guided reviews. <a href="https://docs.github.com/en/copilot/customizing-copilot/adding-repository-custom-instructions-for-github-copilot" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn how to get started</a>.
|
Learn Build status updates of commit 769ca72: ✅ Validation status: passed
For more details, please refer to the build report. |
PR Summary
Fix
.foreach()example so it works in 5.1. The.foreach()and.where()methods were added in PowerShell 4.0. However, they don't apply to all object types. At some point after PowerShell 6.0, the methods were extended to more object types (specifically[pscustomobject]in this example).PR Checklist