summaryrefslogtreecommitdiff
path: root/content/posts/get-type-definition-in-powershell.md
blob: 7348075a11577c1c684fee55a4f7fec3f507adfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
---
title: 'Get type definition in Powershell'
description: "Today I went back to some Powershell scripting with the Az module and it frustrated me that I wasn't easily able to know what properties `Get-AzADGroup` (or any of the other Az cmdlets) returned to me without actually invoking the cmdlet."
tags: ['powershell']
date: 2021-08-27T09:43:57+02:00
draft: false
---

Today I went back to some Powershell scripting with the Az module and it frustrated me that I wasn't easily able to know what properties `Get-AzADGroup` (or any of the other Az cmdlets) returned to me without actually invoking the cmdlet. E.g I dont want to invoke `New-AzADGroup` just to be able to see what properties it will give me so I can use that in my script. Previously I've relied on IntelliSens in my editor, but it often fails, so I sought out to find a more manual solution (who would have thought..).

<!--more-->

Some modules provide information in their help about what type a particular cmdlet returns. We can use this to get information about what that type contains.

If we look at the `Get-AzADGroup` cmdlet as an example. When we run `Get-Help Get-AzADGroup -Full` we can see under the "OUTPUT" section that it returns a `Microsoft.Azure.Commands.ActiveDirectory.PSADGroup` type. To inspect that type we can do the following.

```powershell
PS > Get-Help Get-AzADGroup -Full
...

OUTPUTS
    Microsoft.Azure.Commands.ActiveDirectory.PSADGroup

...
PS > [Microsoft.Azure.Commands.ActiveDirectory.PSADGroup]::new() | gm

   TypeName: Microsoft.Azure.Commands.ActiveDirectory.PSADGroup

Name                 MemberType Definition
----                 ---------- ----------
Equals               Method     bool Equals(System.Object obj)
GetHashCode          Method     int GetHashCode()
GetType              Method     type GetType()
ToString             Method     string ToString()
AdditionalProperties Property   System.Collections.Generic.IDictionary[string,System.Object] AdditionalProperties {get;set;}
DeletionTimestamp    Property   System.Nullable[datetime] DeletionTimestamp {get;set;}
Description          Property   string Description {get;set;}
DisplayName          Property   string DisplayName {get;set;}
Id                   Property   string Id {get;set;}
MailEnabled          Property   System.Nullable[bool] MailEnabled {get;set;}
MailNickname         Property   string MailNickname {get;set;}
ObjectType           Property   string ObjectType {get;}
SecurityEnabled      Property   System.Nullable[bool] SecurityEnabled {get;set;}
Type                 Property   string Type {get;set;}

```

Here we can see what methods and properties we will get if we run `Get-AzADGroup`.

Another solution is to call either the `GetMembers()`, `GetProperties()` or `GetMethods()` on the type which will give you detailed information about each member. It's a little verbose to be frank.. So I prefer the first method.

```powershell
PS > [Microsoft.Azure.Commands.ActiveDirectory.PSADGroup].GetProperties()

MemberType       : Property
Name             : SecurityEnabled
DeclaringType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
ReflectedType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
MetadataToken    : 385876641
Module           : Microsoft.Azure.PowerShell.Cmdlets.Resources.dll
IsCollectible    : False
PropertyType     : System.Nullable`1[System.Boolean]
Attributes       : None
CanRead          : True
CanWrite         : True
IsSpecialName    : False
GetMethod        : System.Nullable`1[System.Boolean] get_SecurityEnabled()
SetMethod        : Void set_SecurityEnabled(System.Nullable`1[System.Boolean])
CustomAttributes : {}

MemberType       : Property
Name             : MailEnabled
DeclaringType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
ReflectedType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
MetadataToken    : 385876642
Module           : Microsoft.Azure.PowerShell.Cmdlets.Resources.dll
IsCollectible    : False
PropertyType     : System.Nullable`1[System.Boolean]
Attributes       : None
CanRead          : True
CanWrite         : True
IsSpecialName    : False
GetMethod        : System.Nullable`1[System.Boolean] get_MailEnabled()
SetMethod        : Void set_MailEnabled(System.Nullable`1[System.Boolean])
CustomAttributes : {}

MemberType       : Property
Name             : MailNickname
DeclaringType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
ReflectedType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
MetadataToken    : 385876643
Module           : Microsoft.Azure.PowerShell.Cmdlets.Resources.dll
IsCollectible    : False
PropertyType     : System.String
Attributes       : None
CanRead          : True
CanWrite         : True
IsSpecialName    : False
GetMethod        : System.String get_MailNickname()
SetMethod        : Void set_MailNickname(System.String)
CustomAttributes : {}

MemberType       : Property
Name             : ObjectType
DeclaringType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
ReflectedType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
MetadataToken    : 385876644
Module           : Microsoft.Azure.PowerShell.Cmdlets.Resources.dll
IsCollectible    : False
PropertyType     : System.String
Attributes       : None
CanRead          : True
CanWrite         : False
IsSpecialName    : False
GetMethod        : System.String get_ObjectType()
SetMethod        :
CustomAttributes : {}

MemberType       : Property
Name             : Description
DeclaringType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
ReflectedType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
MetadataToken    : 385876645
Module           : Microsoft.Azure.PowerShell.Cmdlets.Resources.dll
IsCollectible    : False
PropertyType     : System.String
Attributes       : None
CanRead          : True
CanWrite         : True
IsSpecialName    : False
GetMethod        : System.String get_Description()
SetMethod        : Void set_Description(System.String)
CustomAttributes : {}

MemberType       : Property
Name             : DisplayName
DeclaringType    : Microsoft.Azure.Commands.ActiveDirectory.PSADObject
ReflectedType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
MetadataToken    : 385876650
Module           : Microsoft.Azure.PowerShell.Cmdlets.Resources.dll
IsCollectible    : False
PropertyType     : System.String
Attributes       : None
CanRead          : True
CanWrite         : True
IsSpecialName    : False
GetMethod        : System.String get_DisplayName()
SetMethod        : Void set_DisplayName(System.String)
CustomAttributes : {}

MemberType       : Property
Name             : Id
DeclaringType    : Microsoft.Azure.Commands.ActiveDirectory.PSADObject
ReflectedType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
MetadataToken    : 385876651
Module           : Microsoft.Azure.PowerShell.Cmdlets.Resources.dll
IsCollectible    : False
PropertyType     : System.String
Attributes       : None
CanRead          : True
CanWrite         : True
IsSpecialName    : False
GetMethod        : System.String get_Id()
SetMethod        : Void set_Id(System.String)
CustomAttributes : {}

MemberType       : Property
Name             : Type
DeclaringType    : Microsoft.Azure.Commands.ActiveDirectory.PSADObject
ReflectedType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
MetadataToken    : 385876652
Module           : Microsoft.Azure.PowerShell.Cmdlets.Resources.dll
IsCollectible    : False
PropertyType     : System.String
Attributes       : None
CanRead          : True
CanWrite         : True
IsSpecialName    : False
GetMethod        : System.String get_Type()
SetMethod        : Void set_Type(System.String)
CustomAttributes : {}

MemberType       : Property
Name             : DeletionTimestamp
DeclaringType    : Microsoft.Azure.Commands.ActiveDirectory.PSADObject
ReflectedType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
MetadataToken    : 385876653
Module           : Microsoft.Azure.PowerShell.Cmdlets.Resources.dll
IsCollectible    : False
PropertyType     : System.Nullable`1[System.DateTime]
Attributes       : None
CanRead          : True
CanWrite         : True
IsSpecialName    : False
GetMethod        : System.Nullable`1[System.DateTime] get_DeletionTimestamp()
SetMethod        : Void set_DeletionTimestamp(System.Nullable`1[System.DateTime])
CustomAttributes : {}

MemberType       : Property
Name             : AdditionalProperties
DeclaringType    : Microsoft.Azure.Commands.ActiveDirectory.PSADObject
ReflectedType    : Microsoft.Azure.Commands.ActiveDirectory.PSADGroup
MetadataToken    : 385876654
Module           : Microsoft.Azure.PowerShell.Cmdlets.Resources.dll
IsCollectible    : False
PropertyType     : System.Collections.Generic.IDictionary`2[System.String,System.Object]
Attributes       : None
CanRead          : True
CanWrite         : True
IsSpecialName    : False
GetMethod        : System.Collections.Generic.IDictionary`2[System.String,System.Object] get_AdditionalProperties()
SetMethod        : Void set_AdditionalProperties(System.Collections.Generic.IDictionary`2[System.String,System.Object])
CustomAttributes : {}

```