Skip to content

Fixes #13184 - Fix .foreach() example so it works in 5.1 - #13185

Merged
sdwheeler merged 1 commit into
MicrosoftDocs:mainfrom
sdwheeler:sdw-w601474-i13184-foreach
Jul 30, 2026
Merged

Fixes #13184 - Fix .foreach() example so it works in 5.1#13185
sdwheeler merged 1 commit into
MicrosoftDocs:mainfrom
sdwheeler:sdw-w601474-i13184-foreach

Conversation

@sdwheeler

@sdwheeler sdwheeler commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

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

  • Descriptive Title: This PR's title is a synopsis of the changes it proposes.
  • Summary: This PR's summary describes the scope and intent of the change.
  • Contributor's Guide: I have read the contributor's guide.
  • Style: This PR adheres to the style guide.

Copilot AI review requested due to automatic review settings July 30, 2026 02:29
@learn-build-service-prod

Copy link
Copy Markdown
Contributor

PoliCheck Scan Report

The 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 found

More information about PoliCheck

Information: PoliCheck | Severity Guidance | Term
For any questions: Try searching the learn.microsoft.com contributor guides or post your question in the Learn support channel.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 $myObject initialization.
Comments suppressed due to low confidence (1)

reference/5.1/Microsoft.PowerShell.Core/About/about_Arrays.md:600

  • Changing $myObject to 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 enumerates DictionaryEntry objects (with Key/Value), not singleValue/arrayValue. This means the sample still won’t work as written in 5.1. Define $myObject as a collection (single-item array) containing an object with the expected properties, and also update the later ForEach('Value') / $_.Value references in this example to use arrayValue instead.
$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-service-prod

Copy link
Copy Markdown
Contributor

Learn Build status updates of commit 769ca72:

✅ Validation status: passed

File Status Preview URL Details
reference/5.1/Microsoft.PowerShell.Core/About/about_Arrays.md ✅Succeeded View (powershell-5.1)

For more details, please refer to the build report.

@sdwheeler
sdwheeler merged commit 624cf31 into MicrosoftDocs:main Jul 30, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

code example doesn't work in powershell 5.1 (bug in 5.1)

2 participants