Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I run a PowerShell script when the computer starts?
    primarykey
    data
    text
    <p>I have a PowerShell script that monitors an image folder. I need to find a way to auto run this script after the computer starts.</p> <p>I already tried the following methods, but I couldn't get it working.</p> <ol> <li><p>Use msconfig and add the PowerShell script to startup, but I cannot find the PowerShell script on that list.</p></li> <li><p>Create a shortcut and drop it to startup folder. No luck.</p> <pre><code>%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1" </code></pre> <p>or %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"</p> <p>Here's my PowerShell script:</p> <pre><code>$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 } </code></pre></li> <li><p>I also tried add a basic task using taskschd.msc. It is still not working.</p> <p>Here's what I found, and maybe that will help to debug it.</p> <p>If I open up a PowerShell window and run the script there, it works. But if I run it in a CMD prompt,</p> <pre><code>powershell.exe -File "C:\Doc\Files\FileMonitor.ps1" </code></pre> <p>It will not work. I am not sure it's a permission problem or something else.</p> <p>BTW, I have PowerShell 3.0 installed, and if I type $host.version, it will show 3 there. But my powershell.exe seems like it is still v1.0.</p> <pre><code>%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload