views:

551

answers:

5

Hey guys Just wonderin' if anyone here knows how to display startup items from within a batch file?

+3  A: 

Your startup items are a combination of your own items and the "All Users" items. You can find your own with:

dir "%homedrive%%homepath%\start menu\progams\startup"

and the "All Users" ones (usually) with:

dir "%homedrive%%homepath%\..\all users\start menu\progams\startup"

All you have to do is manipulate the file names to remove the path and the ".lnk" at the end to get the shortcut names.

If you cd to those directories first and just do dir, you won't even have to remove the path.

UPDATE:

If, as your comments seem to indicate, you want all things that start when your system starts, they're found all over the place. You'll want to get a copy of Autoruns (or its command-line brother, Autorunsc, for your purposes).

This is the best approach since it's "blessed" by Microsoft themselves.

paxdiablo
A: 

The 'All Users' doesn't seem to display whats in the MSConfig. Is there anyway to query the registry to get the MSConfig StartUp entries?

@James, this should have been a comment to my answer rather than an "answer" itself. In either case, see my update.
paxdiablo
A: 

As already answered, the startup items are a combination of startup items for "All Users" and the current user. The startup items can either be placed as links in the Start Menu Startup folders, or as registry entries.

For the startup folders, you have already been given the paths in Pax' answer, but do be aware that the paths will be different in non-english versions of Windows.

For the registry, I don't think you can query it using batch script only. It can be done from PowerShell script, if that is an option. Otherwise, I think the easiest way to accomplish the task would be to make a small utility program for the task. (It would be easy to do in one of the .NET languages, or you can do it in C if you cannot rely on the .NET Framework being present).

The Registry entries can be found here, for All Users:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

And for the current user:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

Finally, be aware that the registry also contains the RunOnce key; which contains entries that only should be run once at startup, and then be removed.

driis
A: 

You can use reg command to query appropriate registry key:

reg QUERY HKCU\Software\Microsoft\Windows\CurrentVersion\Run > out

That will give you the list of keys in file "out", like this:

    UIWatcher    REG_SZ    D:\Utils\Ashampoo UnInstaller\UIWatcher.exe
    SpybotSD TeaTimer    REG_SZ    C:\Program Files\Spybot - Search & Destroy\TeaTimer.exe
    SandboxieControl    REG_SZ    "C:\Program Files\Sandboxie\SbieCtrl.exe"
    WMPNSCFG    REG_SZ    C:\Program Files\Windows Media Player\WMPNSCFG.exe

You need to parse this list to get first and second token where REG_SZ is delimiter. If you want to stick to the plain batch language (meh...), which is nothing else but the trouble, you will have to use FOR /F command to get the tokens in manner:

FOR /F "tokens=1,2 delims=$" %i in (out) do @echo %i %j

Before that you will need to replace REG_SZ with some char, lets say $, since FOR doesn't accept string delimiters. The entire process can be put in a single batch file:

SETLOCAL EnableDelayedExpansion

@echo off > StartUp.txt
FOR /F "skip=2 usebackq tokens=* delims= " %%i in (`reg QUERY HKCU\Software\Microsoft\Windows\CurrentVersion\Run`) do (  
    set LINE=%%i
    set LINE=!LINE:REG_SZ=$!
    FOR /F "tokens=1 delims=$" %%j in ('cmd.exe /C echo !LINE!') do echo %%j >> StartUp.txt
)

After you run it StartUp.txt should contain full names of programs, each on its own line. The construct 'cmd.exe /C echo !LINE!' is confusing. I put it as a workaround to the problem I had scanning LINE variable. According to the documentation it should just '!LINE!' but it doesn't work for some reason... In case you are interested in !VAR! syntax, see set /?

Anyway, batch language sucks big time, so its better to avoid it if possible, or to use it in conjuction with some serious tools like awk, sed, autohotkey etc... Each of them can parse the output of reg command in a single regexp line, while autohotkey can create even a nice GUI for it and u can totally avoid batch script.

At the end, dont forget to also can Run section for "All Users".

majkinetor
A: 

If you can use PowerShell, here is a bit of code that uses AUTORUNSC.

$autoruns = [xml](AUTORUNSC -x 2>$null)

foreach ($item in $autoruns.autoruns.item)
{
  If ($item.Description.Length -gt 0)
  { Write-Output $item.Description.Trim() }
  Else
  { Write-Output "n/a"}
  Write-Output $item.ItemName.Trim()
  Write-Output $item.imagepath.Trim()
  "---------------------------------------"
}

EDIT: The -x parameter on AUTORUNSC makes it output XML.

aphoria