How do I create a single char variable in PowerShell?
I am using the following script to format new virtual disks added to our Windows 2016 servers. The problem is, Microsoft does not give any examples of what it wants for the -DriveLetter option. I can't figure out what it wants. A single character, I get that, but why is "s" wrong?
$driveletter = "S"
Get-Disk | Where-Object partitionstyle -eq raw |
Initialize-Disk -PartitionStyle GPT -PassThru |
New-Partition -AssignDriveLetter -DriveLetter $driveletter -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel "App" -Confirm:$false
The error I repeatedly get is:
New-Partition : Invalid Parameter Activity ID: {6fab380c-c0df-4f8d-bdca-f0fed3250238} At line:1 char:22 + ... -Number 2 | New-Partition -AssignDriveLetter -DriveLetter $drivelette ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (StorageWMI:ROOT/Microsoft/Windows/Storage/MSFT_Disk) [New-Partition], CimException + FullyQualifiedErrorId : StorageWMI 5,New-Partition
Once the drive is online I keep trying with:
New-Partition -DiskNumber 2 -AssignDriveLetter -DriveLetter $driveletter -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel "App" -Confirm:$false
To no avail.
Microsoft's documentation on this command says it wants a Char and gives no information on how to give it that.
diskpart
. Can you post your full exception message? If you're trying to pass an explicit char, you can typecast your string:$driveletter = [char]'S'
– Maximilian Burszley-DriveLetter
and-AssignDriveLetter
together.-AssignDriveLetter
will automatically assign a drive letter from the first available letter in the pool. – AdminOfThings