Windows 10: I Need Some Powershell Education

Discus and support I Need Some Powershell Education in Windows 10 Software and Apps to solve the problem; 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... Discussion in 'Windows 10 Software and Apps' started by Wiley Coyote, May 4, 2017.

  1. Pyprohly Win User

    I Need Some Powershell Education


    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”.

    It’s just another program, really.
     
    Pyprohly, May 5, 2017
    #16

  2. 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.
     
    Wiley Coyote, May 5, 2017
    #17
  3. Mystere Win User
    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.
     
    Mystere, May 5, 2017
    #18
  4. I Need Some Powershell Education

    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.
     
    Wiley Coyote, May 5, 2017
    #19
  5. Kari Win User
    To read PATH variable in PowerShell:
    $envI Need Some Powershell Education :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 [​IMG]

    (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 [​IMG]


    Notice that PowerShell does not show new path yet. You need to close PS and open a new session to see changes.
     
  6. Mystere Win User
    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.
     
    Mystere, May 5, 2017
    #21
  7. Superfly Win User
    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...
     
    Superfly, May 5, 2017
    #22
  8. I Need Some Powershell Education

    These last three posts are terrific! Hadn't even considered a couple of things here. Thank you all.
     
    Wiley Coyote, Apr 5, 2018
    #23
Thema:

I Need Some Powershell Education

Loading...
  1. I Need Some Powershell Education - Similar Threads - Need Powershell Education

  2. I need help with removing this Powershell Malware.

    in Windows 10 Gaming
    I need help with removing this Powershell Malware.: Hey i need help with some powershell coding if anyone can know what this code means! it is malware and im trying to know what to do because i have ran the command by accident, I did an antivirus full scan the threat was found it is Trojan:Script/Wacatac.H!ml but in protection...
  3. I need help with removing this Powershell Malware.

    in Windows 10 Software and Apps
    I need help with removing this Powershell Malware.: Hey i need help with some powershell coding if anyone can know what this code means! it is malware and im trying to know what to do because i have ran the command by accident, I did an antivirus full scan the threat was found it is Trojan:Script/Wacatac.H!ml but in protection...
  4. i need window 10 education for my laptop

    in Windows 10 Installation and Upgrade
    i need window 10 education for my laptop: i need window 10 education for my laptop https://answers.microsoft.com/en-us/windows/forum/all/i-need-window-10-education-for-my-laptop/74d2d535-511a-4eaa-b72e-b027ae8d83fe
  5. I need some help with windows powershell for windows terminal

    in Windows 10 Software and Apps
    I need some help with windows powershell for windows terminal: Hello I have a new HP laptop using edge browers and on task manager the edge browser was using 5.6 MB of memory out of 8Mbs so I clicked on the edge and a drop down I saw all the different programs using all the MB's so I closed down windows terminal to free up around 30mb's....
  6. I need some help with windows powershell for windows terminal

    in Windows 10 Gaming
    I need some help with windows powershell for windows terminal: Hello I have a new HP laptop using edge browers and on task manager the edge browser was using 5.6 MB of memory out of 8Mbs so I clicked on the edge and a drop down I saw all the different programs using all the MB's so I closed down windows terminal to free up around 30mb's....
  7. I need some help with windows powershell for windows terminal

    in Windows 10 Gaming
    I need some help with windows powershell for windows terminal: Hello I have a new HP laptop using edge browers and on task manager the edge browser was using 5.6 MB of memory out of 8Mbs so I clicked on the edge and a drop down I saw all the different programs using all the MB's so I closed down windows terminal to free up around 30mb's....
  8. I need some help with windows powershell for windows terminal

    in Windows 10 Software and Apps
    I need some help with windows powershell for windows terminal: Hello I have a new HP laptop using edge browers and on task manager the edge browser was using 5.6 MB of memory out of 8Mbs so I clicked on the edge and a drop down I saw all the different programs using all the MB's so I closed down windows terminal to free up around 30mb's....
  9. I need some assistance

    in Windows 10 Ask Insider
    I need some assistance: I’ve been having a problem with the windows 10 20h2 64bit update it’s constantly failing to update does anyone have a fix? submitted by /u/glowingE [link] [comments] https://www.reddit.com/r/Windows10/comments/k3xqin/i_need_some_assistance/
  10. I need some help.

    in AntiVirus, Firewalls and System Security
    I need some help.: So, I saw a ad that I clicked on. After, as soon that I saw some coding appearing and a Microsoft Alert logo, I immediately clicked off the page. Was that the right thing to do, or should I have used Task Manager to close it? What should I do now?...