Path autocompletion

Welcome Forums General PowerShell Q&A Path autocompletion

Viewing 10 reply threads
  • Author
    Posts
    • #269351
      Participant
      Topics: 1
      Replies: 1
      Points: 16
      Rank: Member

      Hi All.

      I am kinda new to PS, and was able to write relatively simple script to import OpenSSL certificate to Personal store without any issue, but one thing is killing me…

      I am using Read-Host -prompt to get path to certificate file from user and would really like to add bash or CMD-style autocompletion with TAB.

      After few days of futile search on numerous forums and hundreds of try and errors, i decided to ask the professionals…

      Can anyone please help and give me an example of code to autocomplete path to file? Something like this:

      $p12path = Read-Host -Prompt 'Enter path to .p12 certificate file:'

      c:\temp\<TAB>

      No matter what i try, i just get 4 spaces on TAB press. I tend to believe something like this is easy to achieve in PowerShell, but for some reason solution is eluding me…

      Thank you in advance.

      Mike

    • #269357
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      Unfortunately with powershell.exe I don’t think you are going to get that functionality.  Remember the cmdlet you are using is Read-Host which reads a line of input from the user, but when you see the word “Host” in PowerShell you shouldn’t think of the person entering data or the computer as the “Host”.  Host refers to the application hosting the PowerShell library.  If you run Get-Host you will see the current host running PowerShell.  There are several Hosts out there like powershell.exe, powershell ise or even vs code has it’s own host.  I know that is a lot of info and not really solving your problem, but it is important to know that even if there was a parameter in Read-Host that implemented that functionality, it may not be the same depending on the “Host” you are running the script on.  If you really need to control the user experience in your script you might want to use a Windows Form (with PowerShell).  Another option would be to have the user input that as an argument in cmd or powershell i.e.

       

    • #269363
      Participant
      Topics: 9
      Replies: 676
      Points: 2,674
      Helping Hand
      Rank: Community Hero

      I probably misunderstood. I may repost.

    • #269381
      Participant
      Topics: 1
      Replies: 1
      Points: 16
      Rank: Member

      Thank you very much for clarification Mike.

      Unfortunately this script is only single function of a large (estimated 5000 lines) script i am developing for our company’s product installation, so passing path as a parameter is not an option… It is intended to be used by unskilled workers – that’s why i want to make it as errorproof as possible.

      Winform might be a viable option, but it’s absolutely new territory for me. Can you please point me to any tutorial on it? I am thinking to collect variables from windows form and pass them to ps1 with button click…Or maybe you have some kind of template for this use case?

       

      Thank you again.

    • #269384
      Participant
      Topics: 8
      Replies: 168
      Points: 818
      Helping Hand
      Rank: Major Contributor

      With winforms, you could use BrowseForFolder to obtain the cert. This way, the user might be more comfortable and the end result should be more accurate. Manual entry can be prone to error.

       

    • #269435
      Participant
      Topics: 0
      Replies: 6
      Points: 52
      Rank: Member

      Hello,

      If you can’t pass parameter on your script and user is unskilled with CLI environment, you can use OpenFileDialog Object like this :

    • #269492
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      Grezel_ that is exactly what I was thinking. Simple solution using System.Windows.Forms

      Mif97, if you wanted something more custom for unskilled workers, building custom windows forms in PowerShell is really not difficult.  On the free side I would recommend PoshGUI. If you use VSCode like me, I use the PowerShell Pro Tools extensions.  It’s about $100 but worth it in my opinion if you plan on creating lots of custom tools that need a GUI.

    • #269531
      Participant
      Topics: 8
      Replies: 168
      Points: 818
      Helping Hand
      Rank: Major Contributor

      I would like to pose the question of how you manage a 5000+ line script. I too maintain a very large script and am unable to use modules for reasons I wont go into here. I find editing/updating such a large script can be frustrating at best. I use VI (dont laugh) which is nice as it folds on syntax which is a huge help. I tried VSCode with the VI extensions, but VI does not work well in VSCode and VSCode has a bad habit of opening a cached version of the script which is not the latest so I ditched it.

      Anyone care to share how they manage large scripts? Best practices?

    • #269540
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      TonyD05, I use VS Code.  Even when I don’t use modules, I at least try to use functions to break code up into functional pieces and in some cases I use separate .ps1 files and reference them with . operator i.e.

       

    • #269543
      Participant
      Topics: 8
      Replies: 168
      Points: 818
      Helping Hand
      Rank: Major Contributor

      Thanks Mike.

      Do you know how to tell VSCode to NOT load from its cache? I find that if I edit the script using VI, then go back to VSCode, it loads a version from some magic cached location and does not see the changes I made outside of VSCode. I am almost 100% command shell kind of guy and load my script from a shell as such “code .\Script.PS1” and it somehow loads an older version. And if I simply enter “code”, it loads the last script I edited.  There are about 4 billion settings for code and I failed to figure out the one for “dont use the cache” although not having VI is still a deal killer for me.

      Using the VI Extension for VSCode gets you about 90% functionality of VI, but sadly, some of the most important commands don’t work such as the simple . to repeat your last edit/command.

    • #269552
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      I’m definitely not a VS Code master and have only been using it for about a year now.  Before that I was just using the ISE.  I exclusively work in VS Code now so never run into an issue of modifying scripts outside the environment.  I also use git hub extensions in VS Code and it does a great job with version control.

Viewing 10 reply threads
  • You must be logged in to reply to this topic.