Did I do this PowerShell problem correctly? - powershell

Can someone check to see if I did this PowerShell problem correct? Also, if you answer it could please explain what I did wrong. That will help me to continue to learn. Thanks!
Problem 3.) Concatenate a and b so that it creates variable $c such that c -eq "this is the beginning this is the ending" Print the value of variable c.
Here is what I tried and the output was: False
$c = "this is the beginning this is the end"
($a + $b) -eq $c

As per my comment, and my assumptions made.
Clear-Host
$a = 'this is the beginning'
$b = 'this is the end'
$c = 'this is the beginning this is the end'
($a + $b) -eq $c
# Results
<#
($a + $b) -eq $c
False
#>
# Note the space added for the equality of the full string
($a + ' ' + $b) -eq $c
# Results
<#
True
#>
# Or
("$a " + $b) -eq $c
# Results
<#
True
#>
# Single quote is a literal string
# Double quote is normally used for string expansion or PowerShell variable, or formatting requirements.

Related

Multiple Variable Conditions for If Statement in PowerShell

Thanks for the help in this, I think i have over complicated the below but the logic just isn't responding how my mind is telling it too.
Logic in Question:
$a = "One"
$b = "Two"
$c = "Three"
$d = "Four"
If( {$a -and $b} -ne {$c and $d} ) {
Write-Host "Values are Different"
} Else {
Write-Host "values are the same"
}
I want the If statement to run when $a and $b are different to $c and $d, If the are the same see below, I want it to output that the values are the same
$a = "One"
$b = "One"
$c = "One"
$d = "One"
Thanks in advance!
You can use Compare-Object to compare the value pairs as arrays:
if (Compare-Object $a, $b $c, $d -SyncWindow 0) {
'different'
} else {
'same'
}
Note that this is convenient, but relatively slow, which may matter in a loop with many iterations.
The Compare-Object cmdlet compares two arrays and by default returns information about their differences.
-SyncWindow 0 compares only directly corresponding array elements; in other words: $a must equal $c, and $b must equal $d; without -SyncWindow, the array elements would be compared in any order so that 1, 2 would be considered equal to 2, 1 for instance.
Using the Compare-Object call's result as a conditional implicitly coerces the result to a Boolean, and any nonempty result - indicating the presence of at least 1 difference - will evaluate to $True.
As for what you tried:
Use of { ... } in your conditional is not appropriate.
Expressions enclosed in { ... } are script blocks - pieces of code you can execute later, such as with & or .
Even if you used (...) instead to clarify operator precedence (-ne has higher precedence than -and), your conditional wouldn't work as expected, however:
($a -and $b) -ne ($c -and $d) treats all variables as Booleans; in effect, given PowerShell's implicit to-Boolean conversion, you're comparing whether one value pair has at least one empty string to whether the other doesn't.
In addition to the answer from mklement0 and avoiding the rather slow Compare-Object cmdlet:
In what you tried, you will need to compare one specific value with each of the rest of the vales:
($a -eq $b) -and ($a -eq $c) -and ($a -eq $d)
Because the Comparison Operators (-eq) take a higher precedence than the Logical Operators (-and), you can leave the brackets and simplify it to:
$a -eq $b -and $a -eq $c -and $a -eq $d
To make this code DRY and easily expandable for even more values:
if ($a, $b, $c | Where {$_ -ne $d}) {
'different'
} else {
'same'
}
Just remove these {} brackets from the if statement
$a = "One"
$b = "One"
$c = "One"
$d = "One"
If($a -and $b -ne $c -and $d) {
Write-Host "Values are Different"
} Else {
Write-Host "values are the same"
}

reusing variables in foreach loop

Realizing it probably it may not be best practices, is there any other reason to not reuse the current item variable in a foreach as such:
foreach ($a in $something)
{Write-host $a}
.
.
.
foreach ($a in $somethingelse)
{Write-host $a}
In other words, is it always going to be best to in that second case use a different variable ($b?)
As briantist mentions this is fine. However when I do a foreach loop I tend to keep it specified to whatever you're taking it from. I find this will make it easier to comprehend further down the line.
#Example 1
foreach ($item in $array)
{Write-Host $item}
#Example 2
foreach ($user in $userList)
{Write-Host $user}
#Example 3
foreach ($breed in $dogBreed)
{Write-Host $breed}
There's nothing wrong with that. The variable $a will always be set to the current item inside the foreach loop's body.
One reason why this isn't a idea is that variable $a retains it's value (within the assigned scope) until it is reassigned. That may not sound like a big issue, but occasionally PowerShell does something unexpected and it just isn't worth the time troubleshooting to figure something like that out. Consider this function:
function reuse-vars {
$arrayA = 0..3
$arrayB = 10..13
$arrayC = 20..23
foreach ( $a in $arrayA ){
Write-Host "`$a is $a"
Write-Host "`$b is $b"
Write-Host "`$c is $c"
}
foreach ( $b in $arrayB ){
Write-Host "`$a is $a"
Write-Host "`$b is $b"
Write-Host "`$c is $c"
}
foreach ( $c in $arrayC ){
Write-Host "`$a is $a"
Write-Host "`$b is $b"
Write-Host "`$c is $c"
}
}
$a will retain the value from the last foreach iteration until it is reassigned, that includes within nested foreach or if they are in series.
C:\> reuse-vars
$a is 0
$b is
$c is
$a is 1
$b is
$c is
$a is 2
$b is
$c is
$a is 3
$b is
$c is
$a is 3
$b is 10
$c is
$a is 3
$b is 11
$c is
$a is 3
$b is 12
$c is
$a is 3
$b is 13
$c is
$a is 3
$b is 13
$c is 20
$a is 3
$b is 13
$c is 21
$a is 3
$b is 13
$c is 22
$a is 3
$b is 13
$c is 23
It may not matter depending on what you're doing but the possibility for this to cause an issue makes it worthwhile to dream up a different variable name for each foreach (like $b).
I agree with the others that this should work just fine, but that using different names is better practice.
However, if you're paranoid you can always clear or delete the variable manually between the loops:
foreach ($a in $something)
{Write-host $a}
.
$a = $null;
.
foreach ($a in $somethingelse)
{Write-host $a}
Or:
foreach ($a in $something)
{Write-host $a}
.
Remove-Variable -Name a;
.
foreach ($a in $somethingelse)
{Write-host $a}

How to match three or more items

Can't seem to get my head around this. I want to compare three (or more) items - example below - how can I get it to state true? If I do two items it works fine
$a = 2
$b = 2
$c = 2
$a -match $b -match $c
False
Looking at $Matches it only contains two items. I tried brackets around $a and $b but still get the same thing - it keeps on only looking at the first two and ignores the third.
PS C:\Windows\system32> $Matches
Name Value
---- -----
0 2
Your snippet is not working as expected because :
comparing two variables in the first place, gives you true or false,
that means 1 or 0. So, if you'll compare 1 or 0 with 2, it will obviously give false.
In simple terms :
$a -match $b -match $c equates to :
$a -match $b
true
true -match $c
false
So, as Martin has answered, you need to do it this way if you need it for regular expression comparison:
$a -match $b -and $a -match $c
true
But since you are comparing values so you need to use, -eq .
$a -match $b -match $c
actually results in the following:
[bool] $r1 = $a -match $b
[string] $r1 -match $c
Which is probably not what you want. In fact, I'm quite unsure what you actually want. The -match operator performs a regex match of the left operand. Do you perhaps mean something like
$a -eq $b -and $b -eq $c
?
You need to combine your checks using -and. Also use -eq (equals) here:
$a -eq $b -and $a -eq $c

I want to check if an element exist in an array

I want to check if a element exist in an array.
$data = "100400296 676100 582"
$i = "18320-my-turn-582"
if ($data -like $i) { Write-Host "Exist" }
else { Write-Host "Didn't exist" }
This example doesn't work like I want it. $i contains 582, so I want it to be Exist in result.
Your string "18320-my-turn-582" doesn't exist in $data, even though both strings contain the substring 582.
PowerShell treats your strings as a whole, and 18320-my-turn-582 is not present in 100400296 676100 582. To work around this you can:
Use Regex:
$i -match '\d+$'
$data -match $Matches[0]
Split the $i at hyphens so you will have:
$i = $i -split '-'
# turns $i into a array with the elements:
# 18320
# my
# turn
# 582
$data -match $i[-1]
# Output: 100400296 676100 582
Check out Get-Help about_Comparison_Operators to understand the differences between -Contains, -Match and -Like operators.

Check if a string contains any substring in an array in PowerShell

I am studying PowerShell. I want to know how to check if a string contains any substring in an array in PowerShell. I know how to do the same in Python. The code is given below:
any(substring in string for substring in substring_list)
Is there similar code available in PowerShell?
My PowerShell code is given below.
$a = #('one', 'two', 'three')
$s = "one is first"
I want to validate $s with $a. If any string in $a is present in $s then return True. Is it possible in PowerShell?
Using the actual variables in the question for simplicity:
$a = #('one', 'two', 'three')
$s = "one is first"
$null -ne ($a | ? { $s -match $_ }) # Returns $true
Modifying $s to not include anything in $a:
$s = "something else entirely"
$null -ne ($a | ? { $s -match $_ }) # Returns $false
(That's about 25% fewer characters than chingNotCHing's answer, using the same variable names of course :-)
($substring_list | %{$string.contains($_)}) -contains $true
should strictly follow your one-liner
For PowerShell ver. 5.0+
Instead of,
$null -ne ($a | ? { $s -match $_ })
try this simpler version:
$q = "Sun"
$p = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
[bool]($p -match $q)
This returns $True if substring $q is in the array of string $p.
Another Example:
if ($p -match $q) {
Write-Host "Match on Sun !"
}
Michael Sorens' code answer works best to avoid the pitfall of partial substrings matching. It just needs a slight regex modification. If you have the string $s = "oner is first", the code would still return true since 'one' would match 'oner' (a match in PowerShell means the second string contains the first string.
$a = #('one', 'two', 'three')
$s = "oner is first"
$null -ne ($a | ? { $s -match $_ }) # Returns $true
Add some regex for word boundary '\b' and the r on 'oner' will now return false:
$null -ne ($a | ? { $s -match "\b$($_)\b" }) # Returns $false
(I know it's an older thread but at least I might help people looking at this in the future.)
Any response given that uses -match will produce incorrect answers.
Example: $a -match $b will produce false negatives if $b is "."
A better answer would be to use .Contains - but it's case sensitive so you'd have to set all strings to upper or lower case before comparing:
$a = #('one', 'two', 'three')
$s = "one is first"
$a | ForEach-Object {If ($s.toLower().Contains($_.toLower())) {$True}}
Returns $True
$a = #('one', 'two', 'three')
$s = "x is first"
$a | ForEach-Object {If ($s.toLower().Contains($_.toLower())) {$True}}
Returns nothing
You could tweak it to return $True or $False if you'd want, but IMO the above is easier.
I'm amazed that in 6 years nobody has given this more simple and readable answer
$a = #("one","two","three")
$s = "one1 is first"
($s -match ($a -join '|')) #return True
So simply implode the array into a string using vertical bar "|" , as this is the alternation (the "OR" operator) in regex.
https://www.regular-expressions.info/alternation.html
https://blog.robertelder.org/regular-expression-alternation/
Also keep in mind that the accepted answer will not search for exact match. If you want exact match you can use the \b (word boundary)
https://www.regular-expressions.info/wordboundaries.html
$a = #("one","two","three")
$s = "one1 is first"
($s -match '\b('+($a -join '|')+')\b') #return False
It is possible to select a subset of strings containing any of the strings like this:
$array = #("a", "b")
$source = #("aqw", "brt", "cow")
$source | where {
$found = $FALSE
foreach($arr in $array){
if($_.Contains($arr)){
$found = $TRUE
}
if($found -eq $TRUE){
break
}
}
$found
}
One way to do this:
$array = #("test", "one")
$str = "oneortwo"
$array|foreach {
if ($str -match $_) {
echo "$_ is a substring of $str"
}
}