Comma Before Parentheses in Variable AssignmentSetting Windows PowerShell environment variablesWhy does invoking a Powershell script block with .Invoke() return a collection?How do I concatenate strings and variables in PowerShell?How do I get Powershell to output Get-ADUser -Filter * as comma delimited?Azure PowerShell - Object reference not set to an instance of an objectPowershell script to remove line of text from files in folderPowershell Regex to match everything after the first occurrence of a stringNeed Help assigning an embedded Object to a parent Object in a PowerShell scriptReturn Value of a function is not being assigned to the variable in powershellexecute a command before a parameters processing
Why was the LRV's speed gauge displaying metric units?
Why does the Eurostar not show youth pricing?
Problem with Eigenvectors
Is SecureRandom.ints() secure?
A variant of the Multiple Traveling Salesman Problem
Self-deportation of American Citizens from US
Why does Canada require bilingualism in a lot of federal government posts?
A cubeful of three-dimensional devilry
How did the SysRq key get onto modern keyboards if it's rarely used?
Rampant sharing of authorship among colleagues in the name of "collaboration". Is not taking part in it a death knell for a future in academia?
8086 stack segment and avoiding overflow in interrupts
If you inherit a Roth 401(k), is it taxed?
How do I find the FamilyGUID of an exsting database
What would the United Kingdom's "optimal" Brexit deal look like?
Should I accept an invitation to give a talk from someone who might review my proposal?
Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?
Is there a word to describe someone who is, or the state of being, content with hanging around others without interacting with them?
What are the closest international airports in different countries?
What clothes would flying-people wear?
Golden Guardian removed before death related trigger
What is the German equivalent of the proverb 水清ければ魚棲まず (if the water is clear, fish won't live there)?
To find islands of 1 and 0 in matrix
GNU sort stable sort when sort does not know sort order
How to efficiently shred a lot of cabbage?
Comma Before Parentheses in Variable Assignment
Setting Windows PowerShell environment variablesWhy does invoking a Powershell script block with .Invoke() return a collection?How do I concatenate strings and variables in PowerShell?How do I get Powershell to output Get-ADUser -Filter * as comma delimited?Azure PowerShell - Object reference not set to an instance of an objectPowershell script to remove line of text from files in folderPowershell Regex to match everything after the first occurrence of a stringNeed Help assigning an embedded Object to a parent Object in a PowerShell scriptReturn Value of a function is not being assigned to the variable in powershellexecute a command before a parameters processing
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm using the S.DS.P PowerShell module in a PowerShell script of mine. In it, I have to create the following object:
$ADUserEntry = @"distinguishedName"=$null;"objectClass"=$null;"sAMAccountName"=$null;"unicodePwd"=$null;"userAccountControl"=0;
In the documentation of the module, it's stated that I have to do the following assignment to the unicodePwd
field of a variable created using this object:
$obj.unicodePwd = ,([System.Text.Encoding]::Unicode.GetBytes("$randomPassword") -as [byte[]]);
Notice how there's a comma before the first parentheses. What is that comma doing there?
powershell
add a comment |
I'm using the S.DS.P PowerShell module in a PowerShell script of mine. In it, I have to create the following object:
$ADUserEntry = @"distinguishedName"=$null;"objectClass"=$null;"sAMAccountName"=$null;"unicodePwd"=$null;"userAccountControl"=0;
In the documentation of the module, it's stated that I have to do the following assignment to the unicodePwd
field of a variable created using this object:
$obj.unicodePwd = ,([System.Text.Encoding]::Unicode.GetBytes("$randomPassword") -as [byte[]]);
Notice how there's a comma before the first parentheses. What is that comma doing there?
powershell
2
it's thecomma operator
. [grin] it makes an array with the item to the left as an object in the new array. that can be used to prevent the automatic unrolling of an array by wrapping the 1st array in a 2nd one ... and the 2nd one is the one that gets unwrapped. you can see the MS docs on that here ... about_Operators | Microsoft Docs — docs.microsoft.com/en-us/powershell/module/…
– Lee_Dailey
Mar 26 at 20:59
add a comment |
I'm using the S.DS.P PowerShell module in a PowerShell script of mine. In it, I have to create the following object:
$ADUserEntry = @"distinguishedName"=$null;"objectClass"=$null;"sAMAccountName"=$null;"unicodePwd"=$null;"userAccountControl"=0;
In the documentation of the module, it's stated that I have to do the following assignment to the unicodePwd
field of a variable created using this object:
$obj.unicodePwd = ,([System.Text.Encoding]::Unicode.GetBytes("$randomPassword") -as [byte[]]);
Notice how there's a comma before the first parentheses. What is that comma doing there?
powershell
I'm using the S.DS.P PowerShell module in a PowerShell script of mine. In it, I have to create the following object:
$ADUserEntry = @"distinguishedName"=$null;"objectClass"=$null;"sAMAccountName"=$null;"unicodePwd"=$null;"userAccountControl"=0;
In the documentation of the module, it's stated that I have to do the following assignment to the unicodePwd
field of a variable created using this object:
$obj.unicodePwd = ,([System.Text.Encoding]::Unicode.GetBytes("$randomPassword") -as [byte[]]);
Notice how there's a comma before the first parentheses. What is that comma doing there?
powershell
powershell
asked Mar 26 at 20:32
Gonçalo LourençoGonçalo Lourenço
7751 gold badge5 silver badges17 bronze badges
7751 gold badge5 silver badges17 bronze badges
2
it's thecomma operator
. [grin] it makes an array with the item to the left as an object in the new array. that can be used to prevent the automatic unrolling of an array by wrapping the 1st array in a 2nd one ... and the 2nd one is the one that gets unwrapped. you can see the MS docs on that here ... about_Operators | Microsoft Docs — docs.microsoft.com/en-us/powershell/module/…
– Lee_Dailey
Mar 26 at 20:59
add a comment |
2
it's thecomma operator
. [grin] it makes an array with the item to the left as an object in the new array. that can be used to prevent the automatic unrolling of an array by wrapping the 1st array in a 2nd one ... and the 2nd one is the one that gets unwrapped. you can see the MS docs on that here ... about_Operators | Microsoft Docs — docs.microsoft.com/en-us/powershell/module/…
– Lee_Dailey
Mar 26 at 20:59
2
2
it's the
comma operator
. [grin] it makes an array with the item to the left as an object in the new array. that can be used to prevent the automatic unrolling of an array by wrapping the 1st array in a 2nd one ... and the 2nd one is the one that gets unwrapped. you can see the MS docs on that here ... about_Operators | Microsoft Docs — docs.microsoft.com/en-us/powershell/module/…– Lee_Dailey
Mar 26 at 20:59
it's the
comma operator
. [grin] it makes an array with the item to the left as an object in the new array. that can be used to prevent the automatic unrolling of an array by wrapping the 1st array in a 2nd one ... and the 2nd one is the one that gets unwrapped. you can see the MS docs on that here ... about_Operators | Microsoft Docs — docs.microsoft.com/en-us/powershell/module/…– Lee_Dailey
Mar 26 at 20:59
add a comment |
1 Answer
1
active
oldest
votes
As Lee_Daily has pointed out, what you're seeing is the unary form of the (unfortunately named) "comma operator", i.e., PowerShell's array-construction operator.
The unary form creates a single-element array that wraps its (one and only) operand; the array's type is [object[]]
, as usual in PowerShell:
$arr = , 'foo' # wrap string 'foo' in a single-element array
$arr.GetType().Name # the array's type -> 'Object[]'
$arr[0].GetType().Name # the type of the array's one and only element -> 'String'
Note that while you can even wrap arrays that way, PowerShell's operator-precedence rules require a literal array operand to be enclosed in (...)
:
# OK - wraps array 1, 2 in a single-element array.
$arr = , (1, 2)
# !! DOES SOMETHING DIFFERENT:
# Creates a 2-element array whose 1st element is integer 1 wrapped in a
# single-element array
$arr = , 1, 2
The binary form constructs an array from the operands, as expected:
$arr = 1, 2, 3 # 3-element array whose elements are integers 1 and 2 and 3
As an aside, re the specific command shown:
,([System.Text.Encoding]::Unicode.GetBytes("$randomPassword") -as [byte[]])
Neither ,
nor -as [byte[]]
are needed in this scenario, because [System.Text.Encoding]::Unicode.GetBytes()
directly returns a [byte[]]
array.
My pleasure, @GonçaloLourenço; glad to hear it was helpful.
– mklement0
Mar 26 at 21:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55365765%2fcomma-before-parentheses-in-variable-assignment%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As Lee_Daily has pointed out, what you're seeing is the unary form of the (unfortunately named) "comma operator", i.e., PowerShell's array-construction operator.
The unary form creates a single-element array that wraps its (one and only) operand; the array's type is [object[]]
, as usual in PowerShell:
$arr = , 'foo' # wrap string 'foo' in a single-element array
$arr.GetType().Name # the array's type -> 'Object[]'
$arr[0].GetType().Name # the type of the array's one and only element -> 'String'
Note that while you can even wrap arrays that way, PowerShell's operator-precedence rules require a literal array operand to be enclosed in (...)
:
# OK - wraps array 1, 2 in a single-element array.
$arr = , (1, 2)
# !! DOES SOMETHING DIFFERENT:
# Creates a 2-element array whose 1st element is integer 1 wrapped in a
# single-element array
$arr = , 1, 2
The binary form constructs an array from the operands, as expected:
$arr = 1, 2, 3 # 3-element array whose elements are integers 1 and 2 and 3
As an aside, re the specific command shown:
,([System.Text.Encoding]::Unicode.GetBytes("$randomPassword") -as [byte[]])
Neither ,
nor -as [byte[]]
are needed in this scenario, because [System.Text.Encoding]::Unicode.GetBytes()
directly returns a [byte[]]
array.
My pleasure, @GonçaloLourenço; glad to hear it was helpful.
– mklement0
Mar 26 at 21:55
add a comment |
As Lee_Daily has pointed out, what you're seeing is the unary form of the (unfortunately named) "comma operator", i.e., PowerShell's array-construction operator.
The unary form creates a single-element array that wraps its (one and only) operand; the array's type is [object[]]
, as usual in PowerShell:
$arr = , 'foo' # wrap string 'foo' in a single-element array
$arr.GetType().Name # the array's type -> 'Object[]'
$arr[0].GetType().Name # the type of the array's one and only element -> 'String'
Note that while you can even wrap arrays that way, PowerShell's operator-precedence rules require a literal array operand to be enclosed in (...)
:
# OK - wraps array 1, 2 in a single-element array.
$arr = , (1, 2)
# !! DOES SOMETHING DIFFERENT:
# Creates a 2-element array whose 1st element is integer 1 wrapped in a
# single-element array
$arr = , 1, 2
The binary form constructs an array from the operands, as expected:
$arr = 1, 2, 3 # 3-element array whose elements are integers 1 and 2 and 3
As an aside, re the specific command shown:
,([System.Text.Encoding]::Unicode.GetBytes("$randomPassword") -as [byte[]])
Neither ,
nor -as [byte[]]
are needed in this scenario, because [System.Text.Encoding]::Unicode.GetBytes()
directly returns a [byte[]]
array.
My pleasure, @GonçaloLourenço; glad to hear it was helpful.
– mklement0
Mar 26 at 21:55
add a comment |
As Lee_Daily has pointed out, what you're seeing is the unary form of the (unfortunately named) "comma operator", i.e., PowerShell's array-construction operator.
The unary form creates a single-element array that wraps its (one and only) operand; the array's type is [object[]]
, as usual in PowerShell:
$arr = , 'foo' # wrap string 'foo' in a single-element array
$arr.GetType().Name # the array's type -> 'Object[]'
$arr[0].GetType().Name # the type of the array's one and only element -> 'String'
Note that while you can even wrap arrays that way, PowerShell's operator-precedence rules require a literal array operand to be enclosed in (...)
:
# OK - wraps array 1, 2 in a single-element array.
$arr = , (1, 2)
# !! DOES SOMETHING DIFFERENT:
# Creates a 2-element array whose 1st element is integer 1 wrapped in a
# single-element array
$arr = , 1, 2
The binary form constructs an array from the operands, as expected:
$arr = 1, 2, 3 # 3-element array whose elements are integers 1 and 2 and 3
As an aside, re the specific command shown:
,([System.Text.Encoding]::Unicode.GetBytes("$randomPassword") -as [byte[]])
Neither ,
nor -as [byte[]]
are needed in this scenario, because [System.Text.Encoding]::Unicode.GetBytes()
directly returns a [byte[]]
array.
As Lee_Daily has pointed out, what you're seeing is the unary form of the (unfortunately named) "comma operator", i.e., PowerShell's array-construction operator.
The unary form creates a single-element array that wraps its (one and only) operand; the array's type is [object[]]
, as usual in PowerShell:
$arr = , 'foo' # wrap string 'foo' in a single-element array
$arr.GetType().Name # the array's type -> 'Object[]'
$arr[0].GetType().Name # the type of the array's one and only element -> 'String'
Note that while you can even wrap arrays that way, PowerShell's operator-precedence rules require a literal array operand to be enclosed in (...)
:
# OK - wraps array 1, 2 in a single-element array.
$arr = , (1, 2)
# !! DOES SOMETHING DIFFERENT:
# Creates a 2-element array whose 1st element is integer 1 wrapped in a
# single-element array
$arr = , 1, 2
The binary form constructs an array from the operands, as expected:
$arr = 1, 2, 3 # 3-element array whose elements are integers 1 and 2 and 3
As an aside, re the specific command shown:
,([System.Text.Encoding]::Unicode.GetBytes("$randomPassword") -as [byte[]])
Neither ,
nor -as [byte[]]
are needed in this scenario, because [System.Text.Encoding]::Unicode.GetBytes()
directly returns a [byte[]]
array.
answered Mar 26 at 21:21
mklement0mklement0
151k25 gold badges272 silver badges312 bronze badges
151k25 gold badges272 silver badges312 bronze badges
My pleasure, @GonçaloLourenço; glad to hear it was helpful.
– mklement0
Mar 26 at 21:55
add a comment |
My pleasure, @GonçaloLourenço; glad to hear it was helpful.
– mklement0
Mar 26 at 21:55
My pleasure, @GonçaloLourenço; glad to hear it was helpful.
– mklement0
Mar 26 at 21:55
My pleasure, @GonçaloLourenço; glad to hear it was helpful.
– mklement0
Mar 26 at 21:55
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55365765%2fcomma-before-parentheses-in-variable-assignment%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
it's the
comma operator
. [grin] it makes an array with the item to the left as an object in the new array. that can be used to prevent the automatic unrolling of an array by wrapping the 1st array in a 2nd one ... and the 2nd one is the one that gets unwrapped. you can see the MS docs on that here ... about_Operators | Microsoft Docs — docs.microsoft.com/en-us/powershell/module/…– Lee_Dailey
Mar 26 at 20:59