I want PowerShell to throw an error when trying to select non-existing properties, but instead I get empty column as output. Example:
$ErrorActionPreference=[System.Management.Automation.ActionPreference]::Stop;
Set-StrictMode -Version 'Latest'
Get-Process *ex* | Select-Object Id,ProcessName,xxx
Id ProcessName xxx
-- ----------- ---
9084 explorer
11404 procexp
I wrote a script that is importing multiple text files by Import-Csv
, but headers in those file may change, and I'll end up with empty columns being loaded to the system.
EDIT: This is how I'm checking if the headers match:
$csv = Import-Csv -Delimiter ';' -Path $file.FullName
$FileHeaders = @(($csv | Get-Member -MemberType NoteProperty).Name)
if (Compare-Object $ProperHeaders $FileHeaders) {'err'} else {'ok'}
I know that's the way PowerShell works, but Set-StrictMode
documentation was indeed a little misleading for me, as @Matt mentioned. I just wish Select-Object
had some kind of "-NoNewImplicitProps" or "-ReadOnlyPipeline" switch that would do the job for me :). Thanks for the answers.