I Need Some Powershell Education

Page 2 of 3 FirstFirst 123 LastLast

  1. Posts : 1,097
    Windows 10 Home x64 Version 1809 (OS Build 17763.437)
    Thread Starter
       #11

    Pyprohly said:
    CWGilley, could you elaborate?

    If I have a folder “C:\Utilities” in the path variable with batch and powershell scripts in it, I can specify just the name of the batch or powershell script in a powershell console and it will run. Is this what isn’t working for you?
    That's right. None of the C:\Utilities do anything when I try to run them in PS. Here's what I get with $Archive.bat file:

    Windows PowerShell
    Copyright (C) 2016 Microsoft Corporation. All rights reserved.

    PS C:\Users\Clayton> cd\
    PS C:\> $Archive
    PS C:\>


    Even manually taking it to C:\ before the command gets nothing.

    gpglang, thanks for the link. I'm headed over there now.
      My Computer


  2. Posts : 93
    Windows 10
       #12

    CWGilley said:
    Here's what I get with $Archive.bat file:
    ...
    PS C:\> $Archive
    Well there’s your problem. The dollar symbol is used to denote a variable in PowerShell.

    If you want to run that batch file with that special symbol in it, you’ll need to explicitly tell PowerShell you want to invoke it, so quote it and prepend an ampersand (the call operator).
    Code:
    PS> & '$Archive'
      My Computer


  3. Posts : 1,097
    Windows 10 Home x64 Version 1809 (OS Build 17763.437)
    Thread Starter
       #13

    OK, bad example. Here's the simplest (and oldest) one I have named X.bat

    @ECHO Off
    CD\
    C:
    CLS
    Exit


    And it doesn't work either. Your code example does run. Thanks for that one.

    I'm starting to think/understand PS is an environment unto itself. Independent of Windows, CMD, and/or DOS.
      My Computer


  4. Posts : 93
    Windows 10
       #14

    X.bat should clear the console screen, like cls, and that’s all it will do. It will not change the current directory because .bat files will be executed as a sub process in cmd.exe (as the %cmdcmdline% batch variable will reveal to you), if that’s what you mean by “doesn’t work”.

    CWGilley said:
    I'm starting to think/understand PS is an environment unto itself. Independent of Windows, CMD, and/or DOS.
    It’s just another program, really.
      My Computer


  5. Posts : 1,097
    Windows 10 Home x64 Version 1809 (OS Build 17763.437)
    Thread Starter
       #15

    I had expected X.bat to "exit" the shell and return me to the desktop like it does when ran in Command Prompt. But now, thanks to your tutelage I have a much better understanding what PS is & does.

    Thank you for your time. It is much appreciated. Enjoy your weekend.
      My Computer


  6. Posts : 3,257
    Windows 10 Pro
       #16

    CWGilley said:
    OK, bad example. Here's the simplest (and oldest) one I have named X.bat

    @ECHO Off
    CD\
    C:
    CLS
    Exit


    And it doesn't work either. Your code example does run. Thanks for that one.

    I'm starting to think/understand PS is an environment unto itself. Independent of Windows, CMD, and/or DOS.
    Not sure what you mean by "doesn't work". Does it execute and give you different results than you expect? Or does it not execute at all and give an error message?

    You need to be aware that batch files will spawn a separate CMD process and run, which means it will execute the above statements, but the Exit statement will exit the CMD process, leaving you back at the powershell process.

    To be frank, I'm not even sure why you would have such a batch script, since even in a normal command prompt all those actions will be performed and then the command prompt goes away.. you could have just aliased X to Exit and it would do the same thing, the stuff that comes before it are pointless.
      My Computer


  7. Posts : 1,097
    Windows 10 Home x64 Version 1809 (OS Build 17763.437)
    Thread Starter
       #17

    They do not execute, no error message or anything else. The X.bat was created because typing "x" was shorter than "exit" to leave command prompt.

    No big deal and certainly not an "issue" or problem.
      My Computer


  8. Posts : 17,661
    Windows 10 Pro
       #18

    To read PATH variable in PowerShell:
    $env:path
    (#1 in screenshot.)

    -- OR --

    Read it directly from registry:
    Get-ItemProperty 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name PATH
    (#2 in screenshot.)

    I Need Some Powershell Education-2017-05-06_19h15_42.png
    (Click to enlarge.)

    To add a folder in PATH, we need first read current path from registry to a variable (in this example I will use variable $CurrentPath, you can use any variable name you prefer), then write $CurrentPath plus folder we want to add to path to another variable (I will now use $ModifiedPath), and finally replace path in registry with modified path.

    Read current path to variable $CurrentPath:
    $CurrentPath=(Get-ItemProperty 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path

    Add a folder (D:\MyScripts in this example) to path, write new path to variable $ModifiedPath:
    $ModifiedPath=$CurrentPath+';D:\MyScripts'

    Write new path to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path
    Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $ModifiedPath

    I Need Some Powershell Education-2017-05-06_19h26_44.png

    Notice that PowerShell does not show new path yet. You need to close PS and open a new session to see changes.

    Kari
      My Computer


  9. Posts : 3,257
    Windows 10 Pro
       #19

    CWGilley said:
    They do not execute, no error message or anything else. The X.bat was created because typing "x" was shorter than "exit" to leave command prompt.

    No big deal and certainly not an "issue" or problem.
    Yeah, and using an alias for Exit is also just typing a single X.

    Powershell will give you an error if it's not executing, so in fact, it did execute, you just didn't see anything (because of the @ECHO OFF). You can see this by altering your batch file to remove the Exit, and you will see you are still in a CMD shell, and not in a Powershell (there won't be a PS at the beginning). Then type Exit and you will see it returns to the PS prompt.

    Another tell-tale sign is to do a DIR in the CMD prompt vs in Powershell, the output will be different.
    Last edited by Mystere; 06 May 2017 at 13:21.
      My Computer


  10. Posts : 3,453
       #20

    Kari said:
    To read PATH variable in PowerShell:
    $env:
    Yip, that's the way to access those... I don't think the registry route is advisable (for various reasons)

    Anyway.. there is a site called scripting guy or something that dedicate themselves to this stuff...
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 21:31.
Find Us




Windows 10 Forums