Question

I am trying to list output names from win32_share and only include those which do not contain a $. I have the following code written, but it passes everything. How can I list those shares which do not contain a $ in their name?

$winshare = Get-WmiObject -computername 127.0.0.1 -class win32_share | Select-Object name
$share = $null

foreach ($i in $winshare.name) { 
if ($i -notcontains '$') {$share = $i + ',' + $share}else{}
}

write-host $share

Thanks!

Was it helpful?

Solution

Use:

if (!$i.Contains('$'))

-notcontains matches the full string

OTHER TIPS

Since you are querying the localmachine, you could leave out the Computername variable.

Additionally, if you are trying to query for administrative shares, its better to look for share names that ends with the $ character. This can be done with the String.EndsWith() test.

Get-WmiObject -Class win32_share | Where-Object { -not $_.Name.EndsWith('$') }

An alternative would be to use the -notmatch operator:

if ($i -notmatch '\$')

If you only want to get visible shares, you should try

Get-WmiObject -Class Win32_Share -Filter "NOT Name like '%$'"

The filter is run before you get the results, which should be faster, and you can skip the if-test later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top