$psProcess = Get-Process -id $PID
\n$psInstances = (Get-Process -Name $psProcess.name).count
\nif ($psInstances -gt 1) {
\n$psName = “{0}#{1}” -f $psProcess.name,$($psInstances – 1)
\n}
\nelse {
\n$psName = $psProcess.name
\n}<\/p>\n
This is the beginning of a script from Windows - BeatUp-Windows - stress memory and cpu's | Microsoft Learn<\/a><\/p>\n
Can anyone explain what line 4 is doing? Especially the “{0}#{1}” part?<\/p>\n ::Edit:: Added code in-line<\/p>","upvoteCount":7,"answerCount":9,"datePublished":"2017-03-22T18:22:38.000Z","author":{"@type":"Person","name":"chrisciolli","url":"https://community.spiceworks.com/u/chrisciolli"},"acceptedAnswer":{"@type":"Answer","text":" Its a format string, here is the documentation: Standard numeric format strings - .NET | Microsoft Learn<\/a><\/p>\n Its general to .Net, so some of the examples are in other languages.<\/p>\n But the {0} and {1} are placeholders that get replaced with the values following -f in order.<\/p>\n Here is a simple example:<\/p>\n However its real power comes in number manipulation:<\/p>\n $psProcess = Get-Process -id $PID
PS C:\\Users\\cduff> $firstname = 'john'\nPS C:\\Users\\cduff> $lastname = 'doe'\nPS C:\\Users\\cduff> \"{1}, {0}\" -f $firstname,$lastname\ndoe, john\nPS C:\\Users\\cduff> \"{0} {1}\" -f $firstname,$lastname\njohn doe\n<\/code><\/pre>\n
PS C:\\Users\\cduff> \"{0:N2}\" -f 12345.678912345\n12,345.68\nPS C:\\Users\\cduff> \"{0:C}\" -f 1234.56\n$1,234.56\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2017-03-22T18:34:36.000Z","url":"https://community.spiceworks.com/t/help-decipher-a-snippet-of-code/568742/2","author":{"@type":"Person","name":"craigduff","url":"https://community.spiceworks.com/u/craigduff"}},"suggestedAnswer":[{"@type":"Answer","text":"
\n$psInstances = (Get-Process -Name $psProcess.name).count
\nif ($psInstances -gt 1) {
\n$psName = “{0}#{1}” -f $psProcess.name,$($psInstances – 1)
\n}
\nelse {
\n$psName = $psProcess.name
\n}<\/p>\n