1

I am using the following command in PowerShell 2.0 to check if the particular folder is shared or not but I am getting an error.

[bool](Get-WmiObject -Class Win32_Share -ComputerName ravenPC -Filter "Path='D:\websites\website1'") 

Also, I want to store the value of bool in a variable and check it each time if it's true or false. Can someone help me on this.

The error I get is as follows:

Get-WmiObject : Invalid query
At line:1 char:21
+ [bool](Get-WmiObject <<<< -Class Win32_Share -ComputerName ravenPC -Filter "Path='D:\websites\website1'")
     + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
     + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
2
  • Maybe you can use something like this(tested on my local machine) if(Get-WmiObject -Class Win32_Share | Select-String -pattern "yourFolder"){ echo "Is Shared"; }
    – Hackerman
    Oct 11, 2016 at 16:46
  • Bro, this doesn't work. no error, but no output comes :(
    – CodeX
    Oct 11, 2016 at 18:14

1 Answer 1

2

You need to escape the backslashes in your WMI query:

[bool](Get-WmiObject -Class Win32_Share -ComputerName ravenPC -Filter "Path='D:\\websites\\website1'")
2
  • Thanks mate! Also how can I store the value of "true"or "False" returned by bool in a variable? I need to print a string corresponding to that as well
    – CodeX
    Oct 11, 2016 at 18:15
  • Just assign the value to a variable: $var = [bool](...) Oct 11, 2016 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.