I have a PowerShell script that monitors an image folder. I need to find a way to automatically run this script after the computer starts.
I already tried the following methods, but I couldn't get it working.
Use
msconfig
and add the PowerShell script to startup, but I cannot find the PowerShell script on that list.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 }
I also tried to add a basic task using
taskschd.msc
. It is still not working.Here's what I found, and maybe that will help to debug it.
If I open up a PowerShell window and run the script there, it works. But if I run it in a command prompt,
powershell.exe -File "C:\Doc\Files\FileMonitor.ps1"
It will not work. I am not sure it's a permission problem or something else.
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.
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe