$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

Advertisement

This is the beginning of a script from Windows - BeatUp-Windows - stress memory and cpu's | Microsoft Learn<\/a><\/p>\n

Advertisement

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

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

However its real power comes in number manipulation:<\/p>\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":"

$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,"datePublished":"2017-03-22T18:22:38.000Z","url":"https://community.spiceworks.com/t/help-decipher-a-snippet-of-code/568742/1","author":{"@type":"Person","name":"chrisciolli","url":"https://community.spiceworks.com/u/chrisciolli"}},{"@type":"Answer","text":"

^ that<\/p>\n

I’d write it like so:<\/p>\n

$psname = \"$($psProcess.name)#$($psInstances - 1)\"\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2017-03-22T18:39:27.000Z","url":"https://community.spiceworks.com/t/help-decipher-a-snippet-of-code/568742/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

Thanks.<\/p>","upvoteCount":0,"datePublished":"2017-03-22T18:46:57.000Z","url":"https://community.spiceworks.com/t/help-decipher-a-snippet-of-code/568742/4","author":{"@type":"Person","name":"chrisciolli","url":"https://community.spiceworks.com/u/chrisciolli"}},{"@type":"Answer","text":"

If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n

\"codebutton2.png\"<\/p>\n

@chrisciolli<\/a><\/p>","upvoteCount":0,"datePublished":"2017-03-22T18:47:50.000Z","url":"https://community.spiceworks.com/t/help-decipher-a-snippet-of-code/568742/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

I tried to insert the code but it came up blank.<\/p>","upvoteCount":0,"datePublished":"2017-03-22T18:49:57.000Z","url":"https://community.spiceworks.com/t/help-decipher-a-snippet-of-code/568742/6","author":{"@type":"Person","name":"chrisciolli","url":"https://community.spiceworks.com/u/chrisciolli"}},{"@type":"Answer","text":"

\n
\n
<\/div>\n\"\" ChrisC:<\/div>\n
\n

I tried to insert the code but it came up blank.<\/p>\n<\/blockquote>\n<\/aside>\n

IDK then. You tried ¯_(ツ)_/¯ lol<\/p>","upvoteCount":0,"datePublished":"2017-03-22T18:53:15.000Z","url":"https://community.spiceworks.com/t/help-decipher-a-snippet-of-code/568742/7","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

I actually read PLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum!<\/a> before<\/em><\/strong> posting.<\/p>","upvoteCount":4,"datePublished":"2017-03-22T18:57:37.000Z","url":"https://community.spiceworks.com/t/help-decipher-a-snippet-of-code/568742/8","author":{"@type":"Person","name":"chrisciolli","url":"https://community.spiceworks.com/u/chrisciolli"}},{"@type":"Answer","text":"

I should’ve read this earlier, I love looking at scripts. \":slight_smile:\"<\/p>","upvoteCount":0,"datePublished":"2017-03-22T20:56:00.000Z","url":"https://community.spiceworks.com/t/help-decipher-a-snippet-of-code/568742/9","author":{"@type":"Person","name":"kylewisdom2","url":"https://community.spiceworks.com/u/kylewisdom2"}}]}}