@@ -5,28 +5,32 @@ locale: en-us
5
5
keywords : powershell,cmdlet
6
6
title : about_Requires
7
7
---
8
+
8
9
# About Requires
9
10
10
11
## Short description
11
12
Prevents a script from running without the required elements.
12
13
13
14
## Long description
14
15
15
- The ` #Requires ` statement prevents a script from running unless the
16
- PowerShell version, modules (and version), or snap-ins (and version)
17
- prerequisites are met. If the prerequisites are not met, PowerShell
18
- does not run the script.
16
+ The ` #Requires ` statement prevents a script from running unless the PowerShell
17
+ version, modules (and version), or snap-ins (and version) prerequisites are
18
+ met. If the prerequisites aren't met, PowerShell doesn't run the script.
19
19
20
20
### Syntax
21
21
22
22
```
23
+ #Requires -Assembly { <Path to .dll> | <.NET assembly specification> }
23
24
#Requires -Version <N>[.<n>]
24
25
#Requires -PSSnapin <PSSnapin-Name> [-Version <N>[.<n>]]
25
26
#Requires -Modules { <Module-Name> | <Hashtable> }
26
27
#Requires -ShellId <ShellId> -PSSnapin <PSSnapin-Name> [-Version <N>[.<n>]]
27
28
#Requires -RunAsAdministrator
28
29
```
29
30
31
+ For more information about the syntax, see
32
+ [ ScriptRequirements] ( /dotnet/api/system.management.automation.language.scriptrequirements ) .
33
+
30
34
### Rules for use
31
35
32
36
A script can include more than one ` #Requires ` statement. The ` #Requires `
@@ -49,13 +53,30 @@ Example:
49
53
#Requires -Modules Hyper-V
50
54
```
51
55
52
- You might think that the above code should not run because the required module
56
+ You might think that the above code shouldn't run because the required module
53
57
was removed before the ` #Requires ` statement. However, the ` #Requires ` state
54
58
had to be met before the script could even execute. Then the first line of the
55
59
script invalidated the required state.
56
60
57
61
### Parameters
58
62
63
+ #### -Assembly \< Assembly path> | \< .NET assembly specification>
64
+
65
+ Specifies the path to the assembly DLL file or a .NET assembly name. The
66
+ ** Assembly** parameter was introduced in PowerShell 5.0. For more information
67
+ about .NET assemblies, see [ Assembly names] ( /dotnet/standard/assembly/names ) .
68
+
69
+ For example:
70
+
71
+ ```
72
+ #Requires -Assembly path\to\foo.dll
73
+ ```
74
+
75
+ ```
76
+ #Requires -Assembly "System.Management.Automation, Version=3.0.0.0,
77
+ Culture=neutral, PublicKeyToken=31bf3856ad364e35"
78
+ ```
79
+
59
80
#### -Version \< N\> [ .\< n\> ]
60
81
61
82
Specifies the minimum version of PowerShell that the script requires. Enter a
@@ -83,22 +104,22 @@ For example:
83
104
Specifies PowerShell modules that the script requires. Enter the module name
84
105
and an optional version number.
85
106
86
- If the required modules are not in the current session, PowerShell imports
87
- them. If the modules cannot be imported, PowerShell throws a terminating error.
107
+ If the required modules aren't in the current session, PowerShell imports them.
108
+ If the modules can't be imported, PowerShell throws a terminating error.
88
109
89
110
For each module, type the module name (\< String\> ) or a hash table. The value
90
111
can be a combination of strings and hash tables. The hash table has the
91
112
following keys.
92
113
93
114
- ` ModuleName ` - ** Required** Specifies the module name.
94
115
- ` GUID ` - ** Optional** Specifies the GUID of the module.
95
- - It is also ** Required** to specify one of the two below keys. These keys
96
- cannot be used together.
116
+ - It's also ** Required** to specify one of the two below keys. These keys can't
117
+ be used together.
97
118
- ` ModuleVersion ` - Specifies a minimum acceptable version of the module.
98
119
- ` RequiredVersion ` - Specifies an exact, required version of the module.
99
120
100
121
> [ !NOTE]
101
- > ` RequiredVersion ` was added in Windows PowerShell 5.0.
122
+ > ` RequiredVersion ` was added in PowerShell 5.0.
102
123
103
124
For example:
104
125
@@ -127,13 +148,14 @@ the version string you wish to require.
127
148
Get-Module Hyper-V
128
149
```
129
150
130
- ``` output
151
+ ``` Output
131
152
ModuleType Version Name ExportedCommands
132
153
---------- ------- ---- ------------------
133
154
Binary 2.0.0.0 hyper-v {Add-VMAssignableDevice, ...}
134
155
```
135
156
136
- This will ** FAIL** , because "2.0.0" does not exactly match "2.0.0.0"
157
+ The following example fails because ** 2.0.0** doesn't exactly match
158
+ ** 2.0.0.0** .
137
159
138
160
``` powershell
139
161
#Requires -Modules @{ ModuleName="Hyper-V"; RequiredVersion="2.0.0" }
@@ -142,8 +164,9 @@ This will **FAIL**, because "2.0.0" does not exactly match "2.0.0.0"
142
164
#### -ShellId
143
165
144
166
Specifies the shell that the script requires. Enter the shell ID. If you use
145
- the ** ShellId** parameter you must also include the ** PSSnapin** parameter. You
146
- can find current ShellId by querying ` $ShellId ` automatic variable.
167
+ the ** ShellId** parameter, you must also include the ** PSSnapin** parameter.
168
+ You can find the current ** ShellId** by querying the ` $ShellId ` automatic
169
+ variable.
147
170
148
171
For example:
149
172
@@ -156,10 +179,11 @@ For example:
156
179
157
180
#### -RunAsAdministrator
158
181
159
- When this switch parameter is added to your requires statement, it specifies
160
- that the Windows PowerShell session in which you are running the script must
161
- be started with elevated user rights (Run as Administrator).
162
- The RunAsAdministrator parameter is introduced in Windows PowerShell 4.0.
182
+ When this switch parameter is added to your ` #Requires ` statement, it specifies
183
+ that the PowerShell session in which you're running the script must be started
184
+ with elevated user rights. The ** RunAsAdministrator** parameter is ignored on a
185
+ non-Windows operating system. The ** RunAsAdministrator** parameter was
186
+ introduced in PowerShell 4.0.
163
187
164
188
For example:
165
189
@@ -170,7 +194,7 @@ For example:
170
194
### Examples
171
195
172
196
The following script has two ` #Requires ` statements. If the requirements
173
- specified in both statements are not met, the script does not run. Each
197
+ specified in both statements aren't met, the script doesn't run. Each
174
198
` #Requires ` statement must be the first item on a line:
175
199
176
200
``` powershell
@@ -193,4 +217,4 @@ Param
193
217
194
218
[ about_PSSnapins] ( about_PSSnapins.md )
195
219
196
- [ Get-PSSnapin] ( ../Get-PSSnapin.md )
220
+ [ Get-PSSnapin] ( ../Get-PSSnapin.md )
0 commit comments