What is the opposite function of the following?
$tmp = $domain.split(".")
In other words, how do I transfer an array to a string. I've heard of "join", but I can't get it to work.
Non-cmdlet, non-quest, this has to work with PowerShell v1 and all of the older systems!
There's a join operator in Powershell:
$tmp -join "."
$result = [string]::join(".", $array)
In addition to the Join operator (introduced in v2) and the .NET Join method, you can also change the value of the special variable $OFS (Ouptut Field Seperator) to a dot (the default value is a space) and enclose tmp in quotes:
PS> $tmp = "foo.domain.com".split(".")
PS> & {$ofs='.';"$tmp"}
foo.domain.com
In the above example I changed $OFS in a script block to prevent it from changing in the global scope (scriptblocks creates nested scope)
Related
I have an array with data separated by a comma. I need to transpose it so the first part before the comma for each line is joined together by a delimiter as one line and the same for a second part. Example:
AC-2.22,CCI-000012
AC-5.1,CCI-000036
AC-5.3,CCI-001380
I want to have 2 separate variables like so:
variable 1 = AC-2.22; AC-5.1; AC-5.3
Variable 2 = CCI-000012; CCI-000036; CCI-001380
I know this should be simple but I've been staring at code all day and I just want to go eat dinner and goto sleep.
Thanks in advance
Based on the $array of the helpful answer from Santiago Squarzon, you might also use the ConvertFrom-Csv cmdlet to transpose the data using member-access enumeration:
$data = ConvertFrom-Csv $Array -Header var1, var2
$data.var1 -Join ';'
AC-2.22;AC-5.1;AC-5.3
$data.var2 -Join ';'
CCI-000012;CCI-000036;CCI-001380
This is not too hard using .Where method with Split mode:
$array = #(
'AC-2.22,CCI-000012'
'AC-5.1,CCI-000036'
'AC-5.3,CCI-001380'
)
$i = $false
$var1, $var2 = $array.Split(',').Where({ ($i = -not $i) }, 'Split')
$var1 -join '; '
$var2 -join '; '
.Split method works in this case thanks to Member-Access Enumeration, because arrays don't have an .Split method the same is called over each element of the array:
When you use the member-access operator on any object and the specified member exists on that object, the member is invoked. For property members, the operator returns the value of that property. For method members, the operator calls that method on the object.
When you use the member-access operator on a list collection object that doesn't have the specified member, PowerShell automatically enumerates the items in that collection and uses the member-access operator on each enumerated item.
I am attemping to make the Destination from the Copy Item function be the $path and keep running into syntax error.
Function movefiles($dayhash){
foreach ($h in $dayhash.GetEnumerator() )
{
$path = "$formsfolderDaily Checklists\$today_($h.Value)"
Copy-Item $formsfolder$($h.Value) -Destination $formsfolder"Daily Checklists\"$today"_"$($h.Value)
editDate($path)
}
Desired outcome
Function movefiles($dayhash){
foreach ($h in $dayhash.GetEnumerator() )
{
$path = $formsfolder + "Daily Checklists\" + $today + "_" + ($h.Value)
Copy-Item $formsfolder$($h.Value) -Destination $path
editDate($path)
}
$path = "$formsfolderDaily Checklists\$today_($h.Value)"
This makes me think $FormsFolder is a path variable with a trailing backslash -- BUT THAT'S JUST A GUESS -- and one of the reasons Join-Path is so useful.
It's also hard to know what is a literal and what is part of a variblbe name when you start constructing complex expansion strings. I would recommend using the -f (Format operator) which nicely separates the literal and variable portions of your string. My best guess for the above would be:
$path = '{0}Daily Checklists\{1}_{2}' -f $formsfolder, $today, $h.Value
Your template string is on the the left-hand side of the operator, with zero-indexed placeholders in the format {0}, {1}, etc. The placeholders correspond to the variables/expressions/function calls found in the list on the right-hand side of the operator.
It sounds like you want to implement your solution using expandable (double-quoted) strings ("...").
To that end, you need to observe two fundamental rules:
In order to disambiguate variable names from subsequent characters, enclose their names in {...}, e.g. $[today} instead of just $today
Notably, _ is a legitimate character in a PowerShell variable name, so if a _ follows a variable reference {...} is needed too.
In order to embed expressions - such as $h.Value - inside "...", enclose them in $(...), the subexpression operator
You've done this in part in your question, but the first command is missing the $ before (.
For a complete overview of PowerShell's string-interpolation rules, see this answer.
Additionally:
You're using compound tokens composed of unquoted and quoted parts in order to form a single string argument, which is best avoided in PowerShell - see this answer.
Instead, use a single, "..."-enclosed string.
Therefore (the assumption is that the value of $formsfolder ends in \ (or /)):
Function movefiles($dayhash) {
foreach ($h in $dayhash.GetEnumerator() ) {
$path = "${formsfolder}Daily Checklists\$today_$($h.Value)"
Copy-Item "${formsfolder}$($h.Value)" -Destination "${formsfolder}Daily Checklists\${today}_$($h.Value)"
editDate $path
}
I run a cmdlet in powershell which returns a string eg;
123##EXT#outlook.com
456#outlook.com
789#outlook.com
I'm looking for a way to remove the #EXT# characters from the string. I can do everything but remove this and am struggling to find any documentation :/
Lets assume the cmdlet outputs an array of strings. This is simulated by the $adr variable.
#Array of strings
$adr = #(
"123##EXT#outlook.com"
"456#outlook.com"
"789#outlook.com"
)
$adr|foreach {$_ -replace "#EXT#",""}
I've seen the # symbol used in PowerShell to initialise arrays.
What exactly does the # symbol denote and where can I read more about it?
In PowerShell V2, # is also the Splat operator.
PS> # First use it to create a hashtable of parameters:
PS> $params = #{path = "c:\temp"; Recurse= $true}
PS> # Then use it to SPLAT the parameters - which is to say to expand a hash table
PS> # into a set of command line parameters.
PS> dir #params
PS> # That was the equivalent of:
PS> dir -Path c:\temp -Recurse:$true
PowerShell will actually treat any comma-separated list as an array:
"server1","server2"
So the # is optional in those cases. However, for associative arrays, the # is required:
#{"Key"="Value";"Key2"="Value2"}
Officially, # is the "array operator." You can read more about it in the documentation that installed along with PowerShell, or in a book like "Windows PowerShell: TFM," which I co-authored.
While the above responses provide most of the answer it is useful--even this late to the question--to provide the full answer, to wit:
Array sub-expression (see about_arrays)
Forces the value to be an array, even if a singleton or a null, e.g. $a = #(ps | where name -like 'foo')
Hash initializer (see about_hash_tables)
Initializes a hash table with key-value pairs, e.g.
$HashArguments = #{ Path = "test.txt"; Destination = "test2.txt"; WhatIf = $true }
Splatting (see about_splatting)
Let's you invoke a cmdlet with parameters from an array or a hash-table rather than the more customary individually enumerated parameters, e.g. using the hash table just above, Copy-Item #HashArguments
Here strings (see about_quoting_rules)
Let's you create strings with easily embedded quotes, typically used for multi-line strings, e.g.:
$data = #"
line one
line two
something "quoted" here
"#
Because this type of question (what does 'x' notation mean in PowerShell?) is so common here on StackOverflow as well as in many reader comments, I put together a lexicon of PowerShell punctuation, just published on Simple-Talk.com. Read all about # as well as % and # and $_ and ? and more at The Complete Guide to PowerShell Punctuation. Attached to the article is this wallchart that gives you everything on a single sheet:
You can also wrap the output of a cmdlet (or pipeline) in #() to ensure that what you get back is an array rather than a single item.
For instance, dir usually returns a list, but depending on the options, it might return a single object. If you are planning on iterating through the results with a foreach-object, you need to make sure you get a list back. Here's a contrived example:
$results = #( dir c:\autoexec.bat)
One more thing... an empty array (like to initialize a variable) is denoted #().
The Splatting Operator
To create an array, we create a variable and assign the array. Arrays are noted by the "#" symbol. Let's take the discussion above and use an array to connect to multiple remote computers:
$strComputers = #("Server1", "Server2", "Server3")<enter>
They are used for arrays and hashes.
PowerShell Tutorial 7: Accumulate, Recall, and Modify Data
Array Literals In PowerShell
I hope this helps to understand it a bit better.
You can store "values" within a key and return that value to do something.
In this case I have just provided #{a="";b="";c="";} and if not in the options i.e "keys" (a, b or c) then don't return a value
$array = #{
a = "test1";
b = "test2";
c = "test3"
}
foreach($elem in $array.GetEnumerator()){
if ($elem.key -eq "a"){
$key = $elem.key
$value = $elem.value
}
elseif ($elem.key -eq "b"){
$key = $elem.key
$value = $elem.value
}
elseif ($elem.key -eq "c"){
$key = $elem.key
$value = $elem.value
}
else{
Write-Host "No other value"
}
Write-Host "Key: " $key "Value: " $value
}
I have a string in the form -content-, and I would like to replace it with &content&. How can I do this with replace in PowerShell?
PowerShell strings are just .NET strings, so you can:
PS> $x = '-foo-'
PS> $x.Replace('-', '&')
&foo&
...or:
PS> $x = '-foo-'
PS> $x.Replace('-foo-', '&bar&')
&bar&
Obviously, if you want to keep the result, assign it to another variable:
PS> $y = $x.Replace($search, $replace)
The built-in -replace operator allows you to use a regex for this e.g.:
C:\PS> '-content-' -replace '-([^-]+)-', '&$1&'
&content&
Note the use of single quotes is essential on the replacement string so PowerShell doesn't interpret the $1 capture group.