Skip to content

Latest commit

 

History

History
44 lines (31 loc) · 1022 Bytes

UseLiteralInitializerForHashtable.md

File metadata and controls

44 lines (31 loc) · 1022 Bytes
description ms.custom ms.date ms.topic title
Create hashtables with literal initializers
PSSA v1.22.1
06/28/2023
reference
UseLiteralInitializerForHashtable

UseLiteralInitializerForHashtable

Severity Level: Warning

Description

Creating a hashtable using [hashtable]::new() or New-Object -TypeName hashtable without passing a IEqualityComparer object to the constructor creates a hashtable where the keys are looked-up in a case-sensitive manner. However, PowerShell is case-insensitive in nature and it is best to create hashtables with case-insensitive key look-up.

This rule is intended to warn the author of the case-sensitive nature of the hashtable when created using the new method or the New-Object cmdlet.

How to Fix

Create the hashtable using a literal hashtable expression.

Example

Wrong

$hashtable = [hashtable]::new()

Wrong

$hashtable = New-Object -TypeName hashtable

Correct

$hashtable = @{}