about_Data_Sections
Short description
Explains DATA
sections, which isolate text strings and other read-only
data from script logic.
Long description
Scripts that are designed for PowerShell can have one or more DATA
sections
that contain only data. You can include one or more DATA
sections in any
script, function, or advanced function. The content of the DATA
section is
restricted to a specified subset of the PowerShell scripting language.
Separating data from code logic makes it easier to identify and manage both logic and data. It lets you have separate string resource files for text, such as error messages and Help strings. It also isolates the code logic, which facilitates security and validation tests.
In PowerShell, you can use the DATA
section to support script
internationalization. You can use DATA
sections to make it easier to isolate,
locate, and process strings that can be translated into other languages.
The DATA
section was added in PowerShell 2.0 feature.
Syntax
The syntax for a DATA
section is as follows:
DATA [<variable-name>] [-supportedCommand <cmdlet-name>] {
<Permitted content>
}
The DATA
keyword is required. It isn't case-sensitive. The permitted content
is limited to the following elements:
All PowerShell operators, except
-match
If
,Else
, andElseIf
statementsThe following automatic variables:
$PsCulture
,$PsUICulture
,$True
,$False
, and$Null
Comments
Pipelines
Statements separated by semicolons (
;
)Literals, such as the following:
a 1 1,2,3 "PowerShell 2.0" @( "red", "green", "blue" ) @{ a = 0x1; b = "great"; c ="script" } [XML] @' <p> Hello, World </p> '@
Cmdlets that are permitted in a
DATA
section. By default, only theConvertFrom-StringData
cmdlet is permitted.Cmdlets that you permit in a
DATA
section by using the-SupportedCommand
parameter.
When you use the ConvertFrom-StringData
cmdlet in a DATA
section, you can
enclose the key-value pairs in single-quoted or double-quoted strings or in
single-quoted or double-quoted here-strings. However, strings that contain
variables and subexpressions must be enclosed in single-quoted strings or in
single-quoted here-strings so that the variables aren't expanded and the
subexpressions aren't executable.
-SupportedCommand
The SupportedCommand parameter allows you to indicate that a cmdlet or
function generates only data. It's designed to allow users to include cmdlets
and functions in a DATA
section that they have written or tested.
The value of SupportedCommand is a comma-separated list of one or more cmdlet or function names.
For example, the following DATA
section includes a user-written cmdlet,
Format-Xml
, that formats data in an XML file:
DATA -supportedCommand Format-Xml
{
Format-Xml -Strings string1, string2, string3
}
Using a DATA
Section
To use the content of a DATA
section, assign it to a variable and use variable
notation to access the content.
For example, the following DATA
section contains a ConvertFrom-StringData
command that converts the here-string into a hash table. The hash table is
assigned to the $TextMsgs
variable.
The $TextMsgs
variable isn't part of the DATA
section.
$TextMsgs = DATA {
ConvertFrom-StringData -StringData @'
Text001 = Windows 7
Text002 = Windows Server 2008 R2
'@
}
To access the keys and values in hash table in $TextMsgs
, use the following
commands.
$TextMsgs.Text001
$TextMsgs.Text002
Alternately, you can put the variable name in the definition of the DATA
section. For example:
DATA TextMsgs {
ConvertFrom-StringData -StringData @'
Text001 = Windows 7
Text002 = Windows Server 2008 R2
'@
}
$TextMsgs
The result is the same as the previous example.
Name Value
---- -----
Text001 Windows 7
Text002 Windows Server 2008 R2
Examples
Simple data strings.
DATA {
"Thank you for using my PowerShell Organize.pst script."
"It is provided free of charge to the community."
"I appreciate your comments and feedback."
}
Strings that include permitted variables.
DATA {
if ($null) {
"To get help for this cmdlet, type get-help new-dictionary."
}
}
A single-quoted here-string that uses the ConvertFrom-StringData
cmdlet:
DATA {
ConvertFrom-StringData -stringdata @'
Text001 = Windows 7
Text002 = Windows Server 2008 R2
'@
}
A double-quoted here-string that uses the ConvertFrom-StringData
cmdlet:
DATA {
ConvertFrom-StringData -stringdata @"
Msg1 = To start, press any key.
Msg2 = To exit, type "quit".
"@
}
A data section that includes a user-written cmdlet that generates data:
DATA -supportedCommand Format-XML {
Format-Xml -strings string1, string2, string3
}