We help IT Professionals succeed at work.

Further restrict powershell results by drive path

Regarding the previous case (29109977 I am running into another issue. Originally this was completed on a system which only had one set of users in it but we are now looking to run it where individual users need to be further broken up. Attached are the Results.ps1 and SendEmail.ps1 scripts which are in use. This statement specifically is what results in the information we require: Where-Object -Property ShareRelativePath -Match "S2K". However, we need to be able to specify an entire path of C:\Application\S2K1, S2K2, S2K3, etc. Right now it is giving me all of the D:\Application\S2K1, S2K2, etc and other drives. However, I only want to receive one drives information. Please let me know if any further clarification is needed.
Watch Question

Most Valuable Expert 2019
Most Valuable Expert 2018
-match expects a Regular Expression. You're not using any feature that a RegEx offers, so -match is overkill (and can lead to unexpected results if you're not aware that you're working with a RegEx).
Try to use -like and a standard wildcard:
Get-SmbOpenFile | Where-Object {($_.ShareRelativePath -like "C:\Application\S2K1*") -and ($_.ShareRelativePath -notlike "*IMAGES*")} | Sort-Object ClientUsername | Select-Object -ExpandProperty ClientUsername

Open in new window


Interesting. That could explain some weird results we get from time to time. I modified the way you recommended and got the following. Any thoughts?

WARNING: Memory usage of a cmdlet has exceeded a warning level. To avoid this situation, try one of the following: 1)
Lower the rate at which CIM operations produce data (for example, by passing a low value to the ThrottleLimit
parameter), 2) Increase the rate at which data is consumed by downstream cmdlets, or 3) Use the Invoke-Command cmdlet
to run the whole pipeline on the server. The cmdlet that exceeded a warning level of memory usage was started by the
following command line: Get-SmbOpenFile | Where-Object {($_.ShareRelativePath -like "C:\Application\S2K*") -and
($_.ShareRelativePath -notlike "*IMAGES*")} | Sort-Object ClientUsername | Select-Object -ExpandProperty ClientUsername


I was able to implement your instructions successfully. I no longer required the -and for images since I was keying in on a folder specifically. Also I needed to specify Path instead of ShareRelativePath in order to see the whole path. This is working well for me now:

Get-SmbOpenFile | Where-Object {($_.Path -like "C:\Application\S2K*")} | Sort-Object ClientUsername | Select-Object -ExpandProperty ClientUsername


I really appreciate your assistance on this.