How to make one variable , run it on loop with incremental operator in power shell script

this script is to work on partiton and format the drive , using the below script
Get-Disk |
Where partitionstyle -eq ‘raw’
Initialize-Disk -PartitionStyle GPT -PassThru |
New-Partition -UseMaximumSize |
Format-Volume -FileSystem NTFS -AllocationUnitSize 65536 -NewFileSystemLabel “G1500_Mount01” -Confirm:$false

I need to get the filesystem labels as “G1500_Moun01”, “G1500_Mount02” …

You can format strings like so. This includes a leading zero.

for ($index = 1; $index -le 2; $index++) {
    $index_string = '{0:d2}' -f $index
    $label = "G1500_Mount$index_string"
    Write-Output $label
}

Sample output
G1500_Mount01
G1500_Mount02

You could have the loop run off the number of raw disks.

Thanks for your prompt repy, however I am new to powershell scripting an not sure how to use it.


attaching them images, please do the needful.

this is the output

Please do not post images of code as they are not helpful at all. Instead post the plain text formatted as code. This way we can copy and try your code just the way you did. :wink: The same for console outpuit or error messages.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

My code example only shows how you would increment the value appended to a string, in this case what you would use for the label. You would need to use a loop to repeat all the steps minus the first line to format each volume.