Reputation: 127
I am trying filter by the name of each share using $Share.Name
. However, when I try to use -contains
in the if
statement below, I get no results.
The result I want should be
ADMIN$ - C:\ADMIN$
I am working my way to being able to have a variable like:
$ExcludeShares = "ADMIN$"
and filtering based on if the $String.Name
is in $ExcludeShares
I am open to ideas on other ways to filter this.
Thanks in advance!
function GetAllUsedShares{
[psobject]$Shares = Get-WmiObject -Class win32_share
Foreach($Share in $Shares){
$name = [string]$Share.Name
if ($name -contains 'admin'){
Write-Host $Share.Name - $Share.Path
}
}
}
Upvotes: 8
Views: 32161
Reputation: 7327
Case Insensitive Usage
'WINLOGON','Something' -contains 'WinLogon'
True
'WINLOGON','Something' -contains 'WINLOGON'
True
'winlogon' -in 'WINLOGON','Something'
True
Case Sensitive Usage
'WINLOGON','Something' -ccontains 'WinLogon'
False
'WINLOGON','Something' -ccontains 'WINLOGON'
True
'winlogon' -in 'WINLOGON','Something'
True
$Array.Contains('WINLOGON')
Case-Insensitive - All Uppercase
Not needed but can make things all uppercase for comparison purposes.
$All = Get-Process -ComputerName $Target | Select-Object -ExpandProperty Name | Select-Object -Unique
$check = @('winLogon', 'Something')
# not needed but maybe for testing etc.
$All = $All.ToUpper() # e.g. WINLOGON, W3WP, etc
$Check = $Check.ToUpper() #.e.g. WINLOGON, SOMETHING
If ($All -Contains $Check[0]) { ...
Upvotes: 0
Reputation: 27418
Bizarrely, .contains() acts like -contains on arrays:
$a = 'one','two','three'
$a.contains('one')
True
$a.contains('o')
False
$a.contains
OverloadDefinitions
-------------------
bool IList.Contains(System.Object value)
Upvotes: 0
Reputation: 46680
Contains is meant to work against arrays. Consider the following examples
PS C:\Users\Cameron> 1,2,3 -contains 1
True
PS C:\Users\Cameron> "123" -contains 1
False
PS C:\Users\Cameron> "123" -contains 123
True
If you are looking to see if a string contains a text pattern then you have a few options. The first 2 would be -match
operator or the .Contains()
string method
-match
would be one of the simpler examples to use in and If statement. Note: -Match
supports .Net regular expressions so be sure you don't put in any special characters as you might not get the results you expect.
PS C:\Users\Cameron> "Matt" -match "m"
True
PS C:\Users\Cameron> "Matt" -match "."
True
-match
is not case sensitive by default so the first example above returns True. The second example is looking to match any character which is what .
represents in regex which is why it returns True as well.
.Contains()
: -match
is great but for simple strings you can ....
"123".Contains("2")
True
"123".Contains(".")
False
Note that .Contains()
is case sensitive
"asdf".Contains('F')
False
"asdf".Contains('f')
True
Upvotes: 25
Reputation: 472
You can do this in one line:
Get-WmiObject -Class win32_share | where -Property Name -like "*admin*" | % { "$($_.Name) - $($_.Path)" }
Don't forget the asterisks in the where statement. It looks for exact values in that case.
If you want to write it out, this does the same thing:
$shares = Get-WmiObject -Class win32_share
# I pipe the $shares collection into the where-object command to filter on "admin"
$adminshares = $shares | where -property Name -like "*admin*"
# now we can loop with a foreach, which has the "%" operator as it's shorthand in the oneliner
foreach ($share in $adminshares)
{
# variables in strings are a bit weird in powershell, but you have to wrap them like this
write-host "$($share.Name) - $($share.Path)"
}
Upvotes: 1
Reputation: 1
If you are testing $name
for exactly 'admin' you can use the -eq
comparator. This checks if the contents of $name equal the contents of your specified string 'admin'
Upvotes: 0