So I've decided to create this Powershell script.
Github project here.
Version with capturing the process arguments as well.
The actions performed:
- Delete previous file.
- Check the running processes.
- Filter the list by path that contains specific word (Program) which assumes all programs are installed either in Program Files or Program files(x86)
- Get the absolute paths to executables of processes that were found
- Write paths to temp file in temp directory.
- Move temp file to destination directory that is to Startup folder of user.
Pros:
- Written in powershell so the code is native and should work in
Windows Vista and above.
- Works.
Cons:
Programs installed in another path - not under program files - will
not get picked up in this version. That is of course necessary to avoid starting native Windows processes like explorer.exe and so on. However if you have a path like
D:\Apps
where you also install programs, you can modify the line
$_.Path -match "Program"
to
$.Path -match "Program" -or $.Path -match "D:\Apps"
and it will work all the same.
Now the script code:
###############################
# AUTOMATIC STARTUP GENERATOR #
# ASGEN v.1.0 by mnmnc @ 2014 #
###############################
# VARIABLES
# CURRENT PATH
$current = $(pwd).ToString()
# DESTINATION PATH
$destination_path = "$env:userprofile\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\programs.bat"
# MOVING TO TEMP
cd $env:systemroot\Temp
# REMOVING OLD FILE
del $destination_path
# EMPTY ARRAYS
$arr_names = @()
$arr_paths = @()
# FILLING ARRAYS
Get-Process | Where-Object {$_.Path -match "Program" } | foreach { $arr_names += $_.Name; $arr_paths += $_.Path }
# CREATING FILE
for($i = 0; $i -le $arr_names.count -1; $i++)
{
$out = "`"" + $arr_paths[$i] + "`""
write-output $out | out-file "programs.bat" -append -Encoding ascii
}
# INFO
Write-host "`nFolowwing programs will be started on next boot:`n"
cat programs.bat
Write-host "`n"
# MOVING TO STARTUP FOLDER
mv programs.bat "$destination_path"
# RETURNING
cd $current
exit
Simpy copy this code and paste it in the file shutdown.ps1
on your desktop. Save and close this file. Now create a second file shutdown.bat
and put the code
powershell shutdown.ps1
shutdown -s -t 1
in this file. Save and close the bat file. You can now move both files to preferred directory. After that you can click with Right Mouse Button on a bat file and create a shortcut to this file. Then click with Right Mouse Button on a shortcut this time and choose Pin to Start Menu. This will add the shortcut to your Start Menu.
Now every time when you want to close the PC and save the running programs for an automatic startup after boot, you can use this shortcut.
Edit - Including Command Line arguments
Since you would like to save the process command line arguments as well, you have to change the Filling Arrays and Creating file sections to those:
# FILLING ARRAYS
Get-WmiObject Win32_Process | Where-Object { $_.Path -match "Program" } | foreach { $arr_names += $_.Name; $arr_paths += $_.CommandLine; }
# CREATING FILE
for($i = 0; $i -le $arr_names.count -1; $i++)
{
$out = $arr_paths[$i]
write-output $out | out-file "programs.bat" -append -Encoding ascii
}
The change is with using WMI now instead of get-process. Additionally the quotation marks have to be omitted this time.
Notice
In case you have an error when executing the script you probably have incorrect setting for Execution policy for powershell. To change that you have to open powershell console as administrator and type in Set-ExecutionPolicy Unrestricted
and type in Y
when asked to confirm. That's it. Should work fine.
3I don't think Windows does something quite like this natively at least. Have you considered either hibernation (programs won't quit) or using the
Startup
folder (they will be restarted on login)? – a CVn – 2014-07-18T14:48:31.5531Couple of questions that need to be asked: You want to return completely to the previous state so that opened files within some programs will be also opened in this program after restart, or do you want to simply start previously used programs? The second one can be done if you will be willing to use custom shortcut for a shutdown procedure. The first one is realy realy realy unlikely for most of the programs. Sublime text will open automatically previously used files but that is an exception and rather rare behavior. – mnmnc – 2014-07-18T14:51:08.037
My solution would be to create a script that will perform the shutdown procedure. But firstly it will list all the programs that are running and are started by user account, create a list o paths to those executables and place it in the
.bat
file that will be placed in the startup folder of the user. After those actions the script will executeshutdown -s -t 1
and quit. After the boot, all programs that were running will be started again. – mnmnc – 2014-07-18T14:53:43.497@mnmnc I simply want to open the programs not necessarily the files. I have no programming background so how do I use a "custom shortcut for a shutdown procedure"? – Mowing Bar – 2014-07-18T14:59:28.840
@MowingBar Hey. I've created a script just for you. You can see my answer below. The script does just the thing you wanted - saves the list of programs running and starts them after next boot. – mnmnc – 2014-07-18T21:19:36.280