11---
22description : Align assignment statement
3- ms.date : 06/28/2023
3+ ms.date : 03/20/2026
44ms.topic : reference
55title : AlignAssignmentStatement
66---
@@ -10,38 +10,54 @@ title: AlignAssignmentStatement
1010
1111## Description
1212
13- Consecutive assignment statements are more readable if they are aligned. By aligned, we imply that
14- the ` equal ` sign for all the assignment statements should be in the same column .
13+ Consecutive assignment statements are more readable when they're aligned. Assignments are considered
14+ aligned when their ` equals ` signs line up vertically .
1515
16- The rule looks for key (property) value pairs in a hashtable (DSC configuration) to check if they
17- are aligned or not. Consider the following example in which the key value pairs are not aligned.
16+ This rule looks at the key-value pairs in hashtables (including DSC configurations) as well as enum
17+ definitions.
18+
19+ Consider the following example with a hashtable and enum that isn't aligned.
1820
1921``` powershell
2022$hashtable = @{
21- property1 = 'value'
23+ property = 'value'
2224 anotherProperty = 'another value'
2325}
26+
27+ enum Enum {
28+ member = 1
29+ anotherMember = 2
30+ }
2431```
2532
2633Alignment in this case would look like the following.
2734
2835``` powershell
2936$hashtable = @{
30- property1 = 'value'
37+ property = 'value'
3138 anotherProperty = 'another value'
3239}
40+
41+ enum Enum {
42+ member = 1
43+ anotherMember = 2
44+ }
3345```
3446
35- The rule ignores hashtables in which the assignment statements are on the same line. For example,
36- the rule ignores ` $h = {a = 1; b = 2} ` .
47+ The rule ignores any assignments within hashtables and enums which are on the same line as others.
48+ For example, the rule ignores ` $h = @ {a = 1; b = 2} ` .
3749
3850## Configuration
3951
4052``` powershell
4153Rules = @{
4254 PSAlignAssignmentStatement = @{
43- Enable = $true
44- CheckHashtable = $true
55+ Enable = $true
56+ CheckHashtable = $true
57+ AlignHashtableKvpWithInterveningComment = $true
58+ CheckEnum = $true
59+ AlignEnumMemberWithInterveningComment = $true
60+ IncludeValuelessEnumMembers = $true
4561 }
4662}
4763```
@@ -52,8 +68,118 @@ Rules = @{
5268
5369Enable or disable the rule during ScriptAnalyzer invocation.
5470
55- #### CheckHashtable: bool (Default value is ` $false ` )
71+ #### CheckHashtable: bool (Default value is ` $true ` )
5672
5773Enforce alignment of assignment statements in a hashtable and in a DSC Configuration. There is only
58- one switch for hasthable and DSC configuration because the property value pairs in a DSC
74+ one setting for hashtable and DSC configuration because the property value pairs in a DSC
5975configuration are parsed as key-value pairs of a hashtable.
76+
77+ #### AlignHashtableKvpWithInterveningComment: bool (Default value is ` $true ` )
78+
79+ Include key-value pairs in the alignment that have an intervening comment - that is to say a comment
80+ between the key name and the equals sign.
81+
82+ Consider the following:
83+
84+ ``` powershell
85+ $hashtable = @{
86+ property = 'value'
87+ anotherProperty <#A Comment#> = 'another value'
88+ anotherDifferentProperty = 'yet another value'
89+ }
90+ ```
91+
92+ With this setting disabled, the line with the comment is ignored, and it would be aligned like so:
93+
94+ ``` powershell
95+ $hashtable = @{
96+ property = 'value'
97+ anotherProperty <#A Comment#> = 'another value'
98+ anotherDifferentProperty = 'yet another value'
99+ }
100+ ```
101+
102+ With it enabled, the comment line is included in alignment:
103+
104+ ``` powershell
105+ $hashtable = @{
106+ property = 'value'
107+ anotherProperty <#A Comment#> = 'another value'
108+ anotherDifferentProperty = 'yet another value'
109+ }
110+ ```
111+
112+ #### CheckEnum: bool (Default value is ` $true ` )
113+
114+ Enforce alignment of assignment statements of an Enum definition.
115+
116+ #### AlignEnumMemberWithInterveningComment: bool (Default value is ` $true ` )
117+
118+ Include enum members in the alignment that have an intervening comment - that is to say a comment
119+ between the member name and the equals sign.
120+
121+ Consider the following:
122+
123+ ``` powershell
124+ enum Enum {
125+ member = 1
126+ anotherMember <#A Comment#> = 2
127+ anotherDifferentMember = 3
128+ }
129+ ```
130+
131+ With this setting disabled, the line with the comment is ignored, and it would be aligned like so:
132+
133+ ``` powershell
134+ enum Enum {
135+ member = 1
136+ anotherMember <#A Comment#> = 2
137+ anotherDifferentMember = 3
138+ }
139+ ```
140+
141+ With it enabled, the comment line is included in alignment:
142+
143+ ``` powershell
144+ enum Enum {
145+ member = 1
146+ anotherMember <#A Comment#> = 2
147+ anotherDifferentMember = 3
148+ }
149+ ```
150+
151+ #### IncludeValuelessEnumMembers: bool (Default value is ` $true ` )
152+
153+ Include enum members in the alignment that don't have an explicitly assigned value. Enum's don't
154+ need to be given a value when they're defined.
155+
156+ Consider the following:
157+
158+ ``` powershell
159+ enum Enum {
160+ member = 1
161+ anotherMember = 2
162+ anotherDifferentMember
163+ }
164+ ```
165+
166+ With this setting disabled, the third line, which has no value, isn't considered when choosing where
167+ to align assignments. It would be aligned like so:
168+
169+ ``` powershell
170+ enum Enum {
171+ member = 1
172+ anotherMember = 2
173+ anotherDifferentMember
174+ }
175+ ```
176+
177+ With it enabled, the valueless member is included in alignment as if it had a value:
178+
179+ ``` powershell
180+ enum Enum {
181+ member = 1
182+ anotherMember = 2
183+ anotherDifferentMember
184+ }
185+ ```
0 commit comments