I'm not sure what you're getting at, mate. If you specify a volume label that doesn't exist, you get back a null object, which of course does not have a DeviceID property
Correct.
, and you get an error when you try to reference it.
That's only because you have enabled
strict mode in PowerShell. By default, strict mode is disabled. If you are using a
PowerShell profile to enable strict mode,
powershell -NoProfile
will run PowerShell without loading the profile, and strict mode will be disabled in that PowerShell session as a result of this. This is why I specified the
-NoProfile
parameter in the batch script. The profile (if it exists, which, by default, it doesn't) is not needed for the intended purpose [what the OP described].
"It works on my machine" is no comfort to anyone but the person who said it.
It should work on every default, clean, installation of Windows 11. I did specify the
-NoProfile
parameter in the batch script so, it should work in the batch script as-is. (Even, if you have enabled strict mode.) If it turns out that it still doesn't, then you've got more serious problems to worry about.
That said, you can disable strict mode directly in PowerShell by running
Set-StrictMode -Off
if that's what you want. There is no
Get-StrictMode Cmdlet, but you can use this function instead:
function Get-StrictMode { try { $null = ($null -eq @(1)[2]) } catch { return '3.0 or Latest' }; try { ''.Year } catch { return '2.0' }; try { $null = ($undefined -gt 1) } catch { return '1.0' }; return 'Off' }
@pokeefe0001
Sorry for TMI (
Too
Much
Info).