locked
Saving PowerShell Window, to execute when needed RRS feed

  • General discussion

  • Hello Experts, 

    I am new to PowerShell.
    I open PowerShell from Computer and Type  command Like Copy-item C:\Test1 C:\Test2 -recurse

    On PowerShell window ,it looks like below 
    PS C:\Windows> copy-item c:\myfolder c:\myfolder2  -recurse

    This works fine and folders are copied , 

    but how can I save this as .PS1 on my Desktop , so that I can run this when needed by just clicking on it 
    (or Right click on that .PS1 file and Run as PowerShell and Copy command should work) ?

    My Efforts

    I opened Notepad or Notepad++ 
    Copy the content 
    PS C:\Windows> copy-item c:\myfolder c:\myfolder2  -recurse 
    in Notepad and saved it as myCopy.PS1 but when I right click and Execute it, it open Powershell window for less than a sec which has some error(which I cannot see in such a sort span of time) and NO folder get copied 

    • Edited by Rihan8585 Tuesday, July 18, 2017 7:56 AM Edit
    Tuesday, July 18, 2017 7:50 AM

All replies

  • The recommended editor for Powershell scripts is the Powershell_ISE. It has syntax highlithing and many other helpful features.

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Tuesday, July 18, 2017 7:58 AM
  • What's the actual question? How to edit or how to run Powershell scripts?

    By default it is not possible to run Powershell scripts with a double click. If you have a "standard" Windows you could right click and select "Execute with Powershell". 

    If you want to run it with a double click you could use a cmd file to launch or you create a shortcut what includes the Powershell.exe in the command line like this:

    Powershell.exe -ep bypass -file "you script here.ps1"


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Tuesday, July 18, 2017 8:05 AM
  • By default it is not possible to run Powershell scripts with a double click. If you have a "standard" Windows you could right click and select "Execute with Powershell". 

    This is what I am looking for , to run the .PS1 file ,but the question is ,
    how to save the code which execute file when needed ,so that I can execute it by "right click and Execute with PowerShell"

    Tuesday, July 18, 2017 8:48 AM
  • By default it is not possible to run Powershell scripts with a double click. If you have a "standard" Windows you could right click and select "Execute with Powershell". 

    This is what I am looking for , to run the .PS1 file ,but the question is ,
    how to save the code which execute file when needed ,so that I can execute it by "right click and Execute with PowerShell"

    You simply have to write your PowerShell code using a plain text editor (e.g.: Windows Notepad or PowerShell ISE) and save it as a .PS1 file; then you'll be able to right click it and select "Run with PowerShell".
    If you need to keep track of all the executed cmdlets and their ourputs, you have to include the Start-Transcript cmdlet at the beginning of your PowerShell script.

    Bye.


    Luigi Bruno
    MCP, MCTS, MOS, MTA

    Tuesday, July 18, 2017 9:56 AM
  • This is what I am looking for , to run the .PS1 file ,but the question is , how to save the code which execute file when needed ,so that I can execute it by "right click and Execute with PowerShell"

    You just save the code in files with the extension ps1. That's enough. In a freshly installed Windows you can right click ps1 files and select "Run with Powershell" or "Execute with Powershell" or something like this. But if you already installed an application what changed this - the easiest way to run your powershell scripts from the Desktop is to create according shortcuts and include the Powershell executable in the command line like I showed above. 

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Tuesday, July 18, 2017 1:16 PM
    Tuesday, July 18, 2017 1:16 PM
  • Hi Luigi, 

    I copied (as is)
    PS C:\Windows> copy-item c:\myfolder c:\myfolder2  -recurse

    in Notepad and Saved it as myCopy.PS1 but when I right click and Execute as PowerShell, it does not work .

    But same code execute in PowerShell window .

    Do I need to make any change in the myCopy.PS1 code ?

    Wednesday, July 19, 2017 7:21 AM
  • Buddy, you urgently need to take your time and learn the basics of Powershell and how to use Windows and files and so on. We cannot teach you this here in the forum. 

    You're asking Windows beginner questions and that's not what this forum is made for.


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Wednesday, July 19, 2017 7:34 AM
  • Hello Bofh_666, 

    Thanks for the reply.

    If you see my first(top most) thread I already mentioned that " I am new to Power Shell" like beginner  ,Which is why I am seeking help from this forum.( where as I am expert in other field which is not very related to power shell)

    If I was an experts in power shell, I would not have asked this question. 

    If you had shared the link which cover the question/topic I am looking for ,which could solve the issue I am struggling with, It would have help more.

    Thanks for you time and appreciate the effort .



    • Edited by Rihan8585 Wednesday, July 19, 2017 9:19 AM edit
    Wednesday, July 19, 2017 9:15 AM
  • If you had shared the link which cover the question/topic I am looking for ,which could solve the issue I am struggling with, It would have help more.

    That's the point - in my opinion I already did ...  more than once.  ;-)

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Wednesday, July 19, 2017 9:47 AM
  • Hi Luigi, 

    I copied (as is)
    PS C:\Windows> copy-item c:\myfolder c:\myfolder2  -recurse

    in Notepad and Saved it as myCopy.PS1 but when I right click and Execute as PowerShell, it does not work .

    But same code execute in PowerShell window .

    Do I need to make any change in the myCopy.PS1 code ?

    What does "it does not work" mean? Can you see an error message displayed?
    Try executing it in the PowerShell ISE and check for any error message displayed and for the final outcome of the operations performed by the script.

    Bye.


    Luigi Bruno
    MCP, MCTS, MOS, MTA

    Wednesday, July 19, 2017 10:10 AM