PowerShell writing to console out of order

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell writing to console out of order

Post by jvierra »

The following function is s utility function. It does not want to be made to look like a CmdLet. We don't need to add the parameter statements and we don't want it to be complicated.

Code: Select all

function Get-Index {
	param(
		[psobject[]]$Services
	)
    Write-Host 'Returned services:'
    $Services | ForEach-Object{ Write-Host $_.Index $_.Status $_.Name -ForegroundColor green }
    Read-Host "Which service would you like to restart? (0 - $($Services.Count))"
}
It is also bad coding form to use return to exit a function. This should be reserved for special circumstances. PowerShell always returns all output to the pipeline.

Here is a good set of articles on style, formatting, comment usage and basic overall design of a PS script.

PowerShell Style Guide]

PowerShell Best Practices
4lch4_ExtraTxt
Posts: 21
Last visit: Sat Feb 24, 2024 5:20 am
Has voted: 2 times

Re: PowerShell writing to console out of order

Post by 4lch4_ExtraTxt »

Awesome! Thank you so much for the pointers, I'll be sure to read through those resources.

Thanks again.
J.B. Hunt
Devin Leaman
Devin.Leaman@jbhunt.com
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PowerShell writing to console out of order

Post by jvierra »

The links can help you to acquire a consistent style that is generally acceptable across the industry and will help you to sort through much of the, at time, frustrating behavior of the PowerShell scripting system. It is not anything like any programming language. PowerShell is designed to solve many issues of scripting languages and systems and, without a deep understanding, PS can be an uphill battle. Just learning to do things form "recipes" will not teach you the internals of PS and may cause you to make incorrect assumptions. Unfortunately most blogs, articles and simple tutorials do little to actually teach the internals of PS.

"PowerShell in Action - Second Edition" by Bruce Payette is an excellent baseline for PowerShell.

There is also this video tutorial: Getting Started With Microsoft Powershell which is presented by Jefferey Snover who designed the PowerShell language and system.
This topic is 5 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked