none
Powershell Open Windows Search, search for string in specific folder RRS feed

  • Question

  • I have this so far, obtained with a fair amount of help. However, I don't know where to specify a base search location. IE Let's say I want to search only in C:\temp , how would I pass that on? 

    (new-object -com shell.application).FindFiles(); $wsc = (new-Object -ComObject wscript.shell); Start-Sleep -m 200; $wsc.sendkeys("Filename{enter}")
    Tuesday, July 28, 2015 6:33 PM

Answers

  • Got something to work to my liking... Not directly related to the script, but since this is getting called by the VBA I'm writing, I should explain a little I guess... 

         Dim SheetNum As String
         SheetNum = ActiveCell.value
         Call Shell("explorer /root,""search-ms:query=" & SheetNum & "&crumb=location:" & RootDir & "&""")

    RootDir is also a string, set as to a constant directory location. 

    Adapted from a PS command someone provided me with: 

    Set objShell = WScript.CreateObject("WScript.Shell") objShell.Run ("explorer /root,""search-ms:query=exe&crumb=location:C:\Windows&""")


    • Marked as answer by EnriqueI Tuesday, July 28, 2015 9:13 PM
    • Edited by EnriqueI Tuesday, July 28, 2015 9:14 PM
    Tuesday, July 28, 2015 9:12 PM

All replies

  • Hi,

    Why all of that? Use Get-ChildItem instead:

    http://ss64.com/ps/get-childitem.html


    Tuesday, July 28, 2015 6:35 PM
  • Hi,

    Why all of that? Use Get-ChildItem instead:

    http://ss64.com/ps/get-childitem.html


    Thank you for the reply. The reason is that I want to open a GUI, not a command line return. 
    Tuesday, July 28, 2015 6:39 PM
  • What does "I want to open a GUI" mean?

    What is the purpose/goal of the script? In other words, tell what you want to do, not how you think you need to do it.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 28, 2015 6:43 PM
    Moderator
  • What does "I want to open a GUI" mean?

    What is the purpose/goal of the script? In other words, tell what you want to do, not how you think you need to do it.


    -- Bill Stewart [Bill_Stewart]

    I am working in Excel VBA so that when a user clicks on a cell, it passes that cells content as a string. The goal is to open up the search dialog window and automatically search that cell's content in a specified location. The code above does everything except for the very last part (constraining the search to a desired location). As this is for an end user, I want the windows search tool, not a command line / PS DIR return. 
    Tuesday, July 28, 2015 6:50 PM
  • The FindFiles method you are using does not have any parameters, so you can't "type" automatically into the "find" box. It is likely that you simply cannot do this the way you thought you were going to be able to do it, at least not with built-in scripting tools.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 28, 2015 7:11 PM
    Moderator
  • The FindFiles method you are using does not have any parameters, so you can't "type" automatically into the "find" box. It is likely that you simply cannot do this the way you thought you were going to be able to do it, at least not with built-in scripting tools.

    -- Bill Stewart [Bill_Stewart]

    Bill, I respectfully disagree. Run the code in my original post. It will type "Filename" in the search window and run the search. I imagine if there's a way to do that in the search box, there has to be a way to tell it what folder to search in. 


    • Edited by EnriqueI Tuesday, July 28, 2015 7:44 PM
    Tuesday, July 28, 2015 7:43 PM
  • Bill, I respectfully disagree. Run the code in my original post. It will type "Filename" in the search window and run the search. I imagine if there's a way to do that in the search box, there has to be a way to tell it what folder to search in. 

    The issue you'll face is that SendKeys is completely unreliable, as you have no way to force what window or input box has focus to receive your sent keystrokes. It might work while you're testing it, but I guarantee you it'll break later.

    I don't have any real additional information to add here, but I just wanted to point this out so you're at least aware of the possible problems.


    Tuesday, July 28, 2015 7:51 PM
  • Bill, 

    Thank you. I was not aware of that "issue" with SendKeys.

    Tuesday, July 28, 2015 8:35 PM
  • I imagine if there's a way to do that in the search box, there has to be a way to tell it what folder to search in.

    There doesn't have to be a way. Also, as noted, SendKeys is notoriously unreliable.

    As noted: I don't think you're going to be able to get the results you want from built-in scripting tools.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 28, 2015 8:53 PM
    Moderator
  • Got something to work to my liking... Not directly related to the script, but since this is getting called by the VBA I'm writing, I should explain a little I guess... 

         Dim SheetNum As String
         SheetNum = ActiveCell.value
         Call Shell("explorer /root,""search-ms:query=" & SheetNum & "&crumb=location:" & RootDir & "&""")

    RootDir is also a string, set as to a constant directory location. 

    Adapted from a PS command someone provided me with: 

    Set objShell = WScript.CreateObject("WScript.Shell") objShell.Run ("explorer /root,""search-ms:query=exe&crumb=location:C:\Windows&""")


    • Marked as answer by EnriqueI Tuesday, July 28, 2015 9:13 PM
    • Edited by EnriqueI Tuesday, July 28, 2015 9:14 PM
    Tuesday, July 28, 2015 9:12 PM