I've written this code to format Flash Drives:
[CmdLetBinding()]
Param (
[Parameter(Mandatory=$True)]
[String] $DrvLtr,
[Parameter(Mandatory=$False)]
[ ValidateSet("NTFS","ReFS","exFAT",
"FAT32","FAT")]
[String] $FileSys = "NTFS" ,
[Parameter(Mandatory=$False)]
[Switch] $TestIt
)
$FVArgs = @{DriveLetter = $DrvLtr
FileSystem = $FileSys
Force = $True
Full = $True
Confirm = $True
WhatIf = $($TestIt.isPresent)
}
Format-Volume @FVArgs
When I call the code as follows:
PS> .\Dev-New-Format-FlashDrive.ps1 -TestIt -DrvLtr x
It runs as though the -TestIt (value for -WhatIf) and -Confirm parameters to Format-Volume don't exist. The Format-Volume is processed and the drive is formatted.
I ran the code with a break-point at the Volume-Format line and tested the parameters with the following results:
PS> .\Dev-New-Format-FlashDrive.ps1 -TestIt -DrvLtr x
Hit Line breakpoint on 'G:\BEKDocs\Scripts\Dev-New-Format-FlashDrive.ps1:24'
PS> $FVArgs
Name Value
---- -----
FileSystem NTFS
Force True
Confirm True
WhatIf True
DriveLetter x
Full True
PS>
Ideas?
Edited: Theo, in response to your post:
Test Results:
PS> .\Test\Test-Format-FlashDriveOrdered.ps1 -DrvLtr "X" -FileSys FAT32 -TestIt
DriveLetter FriendlyName FileSystemType DriveType HealthStatus OperationalStatu
s
----------- ------------ -------------- --------- ------------ ----------------
X FAT32 Removable Healthy OK
PS> .\Test\Test-Format-FlashDriveMovedSwitches.ps1 -DrvLtr "X" -FileSys FAT32 -TestIt
DriveLetter FriendlyName FileSystemType DriveType HealthStatus OperationalStatu
s
----------- ------------ -------------- --------- ------------ ----------------
X FAT32 Removable Healthy OK
# Force set to False on all the following...
# WhatIf Removed
PS> .\Test\Test-Format-FlashDrive.ps1 -DrvLtr "X" -FileSys FAT32 -TestIt1
DriveLetter FriendlyName FileSystemType DriveType HealthStatus OperationalStatu
s
----------- ------------ -------------- --------- ------------ ----------------
X FAT32 Removable Healthy OK
# Whatif Replaced Confirm Removed
PS> .\Test\Test-Format-FlashDrive.ps1 -DrvLtr "X" -FileSys FAT32 -TestIt
DriveLetter FriendlyName FileSystemType DriveType HealthStatus OperationalStatu
s
----------- ------------ -------------- --------- ------------ ----------------
X FAT32 Removable Healthy OK