Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, January 1, 2017

how to run PowerShell Script when computer starts?

how to run PowerShell Script when computer starts?


I have a powershell script that monitor an image folder. I need to find a way to auto run this script after computer starts.

already tried following methods, coudln't get it working.

  1. use msconfig and add powershell script to startup, but I cannot find powershell script on that list.

  2. create a shortcut and drop it to startup folder. no luck.

    %SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"

    or %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"

here's my powershell script:

$folder = "C:\\Doc\\Files"  $dest = "C:\\Doc\\Files\\images"  $filter = "*.jpg"    $fsw = new-object System.IO.FileSystemWatcher $folder, $filter -Property @{      IncludeSubDirectories=$false      NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'  }    $onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {        Start-Sleep -s 10      Move-Item -Path C:\Doc\Files\*.jpg C:\Doc\Files\images  }  

UPDATE:

  1. also tried add a basic task using taskschd.msc. still not working.

here's what I found, maybe that will help to debug it.

if I open up my powershell and run the script there, it works. but if I run it in CMD prompt.

powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"  

it will not work. not sure its permission or something else.

btw, I have powershell 3.0 install, if i type $host.version, it will show 3 there. but my powershell.exe seems like still v1.0

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe  

Answer by mjolinor for how to run PowerShell Script when computer starts?


You could set it up as a Scheduled Task, and set the Task Trigger for "At Startup"

Answer by Knuckle-Dragger for how to run PowerShell Script when computer starts?


Copy ps1 into this folder, create it if necessary. It will run every start-up. (before user logon occurs)

C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup  

Also can be done through GPEDIT.msc if available on your OS build (lower level OS maybe not).

Answer by Helpy-Helperton for how to run PowerShell Script when computer starts?


I finally got my PowerShell script to run automatically on every startup. You will need to create two files: the first is the Powershell script (e.g. script.ps1) and the second is a .cmd file that will contain commands that will run on the command prompt (e.g. startup.cmd).

The second file is what needs to be executed when the computer starts up, and simply copy-pasting the .ps1 to the startup folder won't work, because that doesn't actually execute the script - it only opens the file with Notepad. You need to execute the .cmd which itself will execute the .ps1 using PowerShell. Ok, enough babbling and on to the steps:

  1. Create your .ps1 script and place it in a folder. I put it on my desktop for simplicity. The path would look something like this:

C:\Users\<user_name>\Desktop\script.ps1

  1. Create a .cmd file and place it in

C:\Users\<user_name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\startup.cmd

Doing this will execute the cmd file every time on startup. Here is a link of how to create a .cmd file if you need help.

  1. Open the .cmd file with a text editor and enter the following lines:

    PowerShell -Command "Set-ExecutionPolicy Unrestricted" >> "%TEMP%\StartupLog.txt" 2>&1 PowerShell C:\Users\\Desktop\script.ps1 >> "%TEMP%\StartupLog.txt" 2>&1

This will do two things:

  1. Set the Execution Policy of your PowerShell to Unrestricted. This is needed to run scripts or else PowerShell will not do it.
  2. Use PowerShell to execute the .ps1 script found in the path specified.

This code is specifically for PowerShell v1.0. If you're running PowerShell v2.0 it might be a little different. In any case, check this source for the .cmd code.

  1. Save the .cmd file

Now that you have your .ps1 and .cmd files in their respective paths and with the script for each, you are all set.

Answer by Dan for how to run PowerShell Script when computer starts?


I have a script that starts a file system watcher as well, but once the script window is closed the watcher dies. It will run all day if I start it from a powershell window and leave it open, but the minute I close it the script stops doing what it is supposed to.
You need to start the script and have it keep powershell open.
I tried numerous ways to do this, but the one that actually worked was from http://www.methos-it.com/blogs/keep-your-powershell-script-open-when-executed

param ( $Show )  if ( !$Show )   {      PowerShell -NoExit -File $MyInvocation.MyCommand.Path 1      return  }  

Pasting that to the top of the script is what made it work.
I start the script from command line with

powershell.exe -noexit -command "& \path\to\script.ps1"

Answer by Manmeet Gill for how to run PowerShell Script when computer starts?


What I do is create a shortcut that I place in shell:startup.

The shortcut has the following:

Target: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "C:\scripts\script.ps1"

(replacing scripts\scripts.ps1 with what you need)

Start In: C:\scripts

(replacing scripts with folder which has your script)


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.