Some basic PS questions:

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Olaf Soyk Olaf Soyk 3 weeks, 1 day ago.

  • Author
    Posts
  • #71951
    Profile photo of Peter
    Peter
    Participant

    Sorry for these newbie questions but I am new to Powershell.

    1.) How do I find out my current Powershell version (under 64bit Win 7)?

    2.) What is the newest PowerShell version and how do I download it (not through MS update channel)?

    3.) Do PowerShell script files have a certain file extension which (when double clicked) will be executed automatically (similar to traditional *.bat files)?

    4.) If I start powershell from START menu a certain start path is shown.

    Is there a way to adjust this path to another directory? (or even better automatically to the last used)?

    Thank you

    Peter

  • #71954
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Peter,

    welcome to the brighter world of Powershell scripting. 😉 But let me ask something first: Are you familiar with the concept of internet search engines?

    1.) How do I find out my current Powershell version (under 64bit Win 7)?

    You open a Powershell console or a Powershell ISE and "evaluate" the following variable like this:

    $PSVersionTable

    Tip: You can start typing "$PSV" and hit Tab – Powershell will extend the variable to its proper name for you.

    2.) What is the newest PowerShell version and how do I download it (not through MS update channel)?

    Something about 5.1. ..... it even depends on the Windows version you're working with

    3.) Do PowerShell script files have a certain file extension which (when double clicked) will be executed automatically (similar to traditional *.bat files)?

    Yes. "*.ps1" BUT You cannot run Powershell scripts with a double click. If you need to do something like this you can use shortcuts with the complete command line to run Powershell console including the script path.

    4.) If I start powershell from START menu a certain start path is shown. ...

    Yes. The Powershellest way to set your environment to your needs would be to create a profile.

    Have a lot of fun!

  • #72055
    Profile photo of figueroa2david
    figueroa2david
    Participant

    Hopefully these will be a little clearer..

    2. PS 5.1 is the most current (since you did mention Windows 7 x64) – you can download it from Microsoft https://msdn.microsoft.com/en-us/powershell/wmf/5.1/install-configure

    3. No — you can't automatically launch the powershell scripts like batch files. You have to call them from powershell, or a batch file, or a shortcut file etc. Your command line to use would be powershell.exe -file , and you would use that for your shortcut, batch file, etc.

    4. You will want to create a powershell profile — the profile is just a powershell script, but it has a specific name and exists in a specific location. Check this link: https://blogs.technet.microsoft.com/heyscriptingguy/2012/05/21/understanding-the-six-powershell-profiles/. But, since you want to have it remember the last location, then you need to specifically record the path.
    Something like this might accomplish what you are looking for (in your Powershell profile).. it's basically 2 phases. at start up and at closing..

    Something like this might work.. (might need to be tweaked)..
    The first 2 lines handle changing the path when you first start powershell
    The third line creates an event watcher looking for the powershell host to close, and when it closes gracefully, it stores the value in the root of the user's profile (paths could be changed).

    $LastUsedPath = Get-ItemProperty -path HKCU:\ -name LastUsedPath
    if ($LastUsedPath) { set-location $LastUsedPath }
    Register-EngineEvent PowerShell.Exiting -Action { set-itemproperty -path HKCU:\ -name LastUsedPath -value $PWD }
    

    David F.

    • #72067
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      Regardless of whether I would consider this approach as a little bad style or not. I suspect your code snippet would not work right away. At least not without a little modification or preliminary work.
      Because the regsitry key value you mentioned is not a standard on Windows it does not exist until you create it. So either you have to create it first or you have to add a "-Force" to the "Set-ItemProperty" cmdlet in your snippet.
      And on top of that – your approach would potentially cause Powershell to start every time with another location. I'm sceptic that it is what Peter asked for.
      Até logo 😉

You must be logged in to reply to this topic.