Powershell scripts to quickly start multiple specified windows services

Question:

Setting the boot start of a specified application is not the result I want, it lacks a lot of selectivity. Trying to implement a small daily feature myself using powershell, window laptop boots up and quickly opens the specified apps.

Most of the other apps open perfectly, but there are three apps (tailscale, vscode, obsidian) that always appear to shut down when powershell is turned off after execution.

I’m wondering if anyone has any ideas on how to do this?

The script I use is as follows:

  1. get the location of the shortcut files in the specified directory, so that you can choose which ones you want to customize the quick step2. according to the result of step 1, pick which apps you want to customize.

getDesktopAllShortPath.ps1

# Define the directory to be searched
$targetDirectory = "d:\desktop"

# Get the current directory of the script
$currentDirectory = Get-Location

# Define the output file path
$outputFile = Join-Path -Path $currentDirectory -ChildPath "colShorts.txt"

# Find all .lnk files
$shortcuts = Get-ChildItem -Path $targetDirectory -Recurse -Filter *.lnk

# Save the shortcut file path to a file
$shortcuts.FullName | Out-File -FilePath $outputFile -Encoding UTF8

Write-Host "The shortcut file path has been saved to $outputFile"
  1. According to the result of step 1, I deleted all the normal apps, and only kept the three apps that are doubtful in the following code.

getStartProcess.ps1

$shortcutFiles = @(
    "D:\desktop\Obsidian.lnk",
    "D:\desktop\Visual Studio Code.lnk",
    "D:\desktop\Tailscale.lnk"
)

# Iterate through each shortcut file
foreach ($shortcutFile in $shortcutFiles) {
    # Get the path to the application to which the shortcut file points
    $appPath = (New-Object -ComObject WScript.Shell).CreateShortcut($shortcutFile).TargetPath

    # Check if the application is already running
    if (-not (Get-Process | Where-Object { $_.MainModule.FileName -eq $appPath })) {
        # If the application is not running, start the application
        Start-Process $appPath
    } else {
        Write-Host "The application $appPath is already running and does not need to be opened again."
    }
}

The error log is as follows:

PowerShell 7.4.2
PS C:\Program Files\PowerShell\7> cd
PS C:\Users\auggie> .\getStartProcess.ps1
***
The application C:\Program Files\Mozilla Firefox\firefox.exe is already running and does not need to be opened again.
***
2024-06-04 01:17:42 Loading updated app package C:\Users\auggie\AppData\Roaming\obsidian\obsidian-1.5.12.asar
2024-06-04 01:17:42 Checking for update using Github
The application C:\Program Files\VanDyke Software\Clients\SecureCRT.exe is already running and does not need to be opened again.
2024-06-04 01:17:43 Success.
2024-06-04 01:17:43 Latest version is 1.5.12
2024-06-04 01:17:43 App is up to date.

[main 2024-06-04T01:17:44.749Z] update#setState idle
logtail started
Program starting: v1.66.4-t067defc64-g49481b7d0, Go 1.22.3: []string{"C:\\Program Files\\Tailscale\\tailscale-ipn.exe"}
LogID: be7ecdf60303c188bdfa054d60f440ac53f0e0b9c28194df8d9bff586956ba8b
logpolicy: using LocalAppData dir C:\Users\auggie\AppData\Local\Tailscale
stdout ok.
stderr ok.
log ok.
Running UI client...
ui: creating mainWindow
ui: creating tray
ui: watchBackend: connected.
ui: Handle: logs: be:995e1c9d41456dae3bfe7252f5a4ab263ce0cd646c9e975c27b0df6be3efd01b fe:be7ecdf60303c188bdfa054d60f440ac53f0e0b9c28194df8d9bff586956ba8b
ui: ui: latest version check: "1.66.4", <nil>
PS C:\Users\auggie> [24928:0604/091747.329:ERROR:jump_list.cc(300)] Failed to append custom category 'Recently used folders' to Jump List due to system privacy settings.
[main 2024-06-04T01:17:47.349Z] updateWindowsJumpList#setJumpList unexpected result: customCategoryAccessDeniedError
ui: ui: Notify: running latest version
ui: ui: setLatestVersion("1.66.4", "", false)
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: started taildrive file server at: d6b3b148de9819bec747ca414a56dd46e46c10a0298f2cd50162abdc85ae0b95|127.0.0.1:9810
ui: setting taildrive file server address
ui: set taildrive file server address
ui: Beginning to watch engine updates
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
                         ^C
PS C:\Users\auggie>
PS C:\Users\auggie> ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later

PS C:\Users\auggie> ^C
PS C:\Users\auggie>
PS C:\Users\auggie>
PS C:\Users\auggie> ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later

PS C:\Users\auggie>
PS C:\Users\auggie> ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
                         ^C
PS C:\Users\auggie>
PS C:\Users\auggie>
PS C:\Users\auggie> ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
[main 2024-06-04T01:18:14.759Z] update#setState checking for updates
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
[main 2024-06-04T01:18:16.219Z] update#setState idle
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later
ui: cacheExitNodeInfo: getSuggestedExitNode failed: unable to suggest an exit node, try again later

These all look like tailscale errors and not Powershell. My guess is tailscale is attached to the Powershell process and subsequently dies when it dies. If you remove it and try just vscode and Obsidian I’m guessing it will work just fine

Tested it, doesn’t work. obsidian also continuously goes to check for updates in powershell terminal, and vscode’s subprocesses seem to interfere a bit. Close powershell,obsidian and vscode are automatically shut down.

What about using the call operator, ‘&’, on either the .lnk or the .exe
https://ss64.com/ps/call.html

I haven’t tested this but I’m curious. I’ll see if i can try this out
EDIT: I don’t have tailscales or Obsidian to test, but I put VS code on a Windows machine and tested using the call operator and Start-Process. Ultimately it always seems to tie the VS Code process to that powershell instance and if I close Powershell, Code closes too. However, the command “code” is available in your path, and launches VS Code and successfully separates it from the powershell process.
I know that doesn’t help with tailscales and obsidian. Sorry

That’s okay, it’s been a pleasure to receive your enthusiastic replies.

I did some testing. Looks like some processes do not detach from the Powershell parent. Vscode was what I tested. A work-around would be to start them as jobs.

Start-Job -Scriptblock { "C:\PATH\TO\Microsoft VS Code\Code.exe" }
1 Like

After testing, the final answer is as follows, vscode is not placed inside the for loop, it is mentioned separately.

Putting vscode in a for loop will spawn dirty vscode processes, and closing the powershell terminal will automatically shut down any vscode that is already open.

The debugging code has been left commented out for reference only.

Putting the vscode on its own outside of the terminal is not a problem. Repeatedly running it will open the vscode again and again.
This is already the best answer, but i’ll optimize it later.
thanks folks.

$shortcutFiles = @(
    "D:\desktop\Firefox.lnk",
    "D:\desktop\Obsidian.lnk",
    "D:\desktop\SecureCRT 9.0.lnk",
    "D:\desktop\Tailscale.lnk",
)
#"D:\desktop\Visual Studio Code.lnk",

# Iterate through each shortcut file
foreach ($shortcutFile in $shortcutFiles) {
    # Get the path to the application to which the shortcut file points
    $appPath = (New-Object -ComObject WScript.Shell).CreateShortcut($shortcutFile).TargetPath

	
	
    # Check if the application is already running
    if (-not (Get-Process | Where-Object { $_.MainModule.FileName -eq $appPath })) {
        # If the application is not running, start the application using Start-Job
        switch -wildcard ($appPath) {
            #"*Visual Studio Code*" {
                ## Start-Job -ScriptBlock { param($path) Start-Process -FilePath $path } -ArgumentList $appPath
				#Start-Process -NoNewWindow -FilePath "code"
				#Start-Job -ScriptBlock { Start-Process -FilePath "code" -NoNewWindow} | Out-Null
				#Write-Output $path
				#Write-Output $_.MainModule.FileName
				##Start-Process -FilePath $path -NoNewWindow
				##Start-Job -ScriptBlock { Start-Process -FilePath "code" -NoNewWindow}
				#Start-Job -ScriptBlock { param($path) Start-Process -FilePath $path } -ArgumentList $shortcutPath
				#Start-Job -ScriptBlock { param($path) Start-Process -FilePath $path } -ArgumentList $appPath  -ErrorAction SilentlyContinue 2>$null
				#Start-Job -ScriptBlock { param($path) Start-Process -FilePath $path 2>$null } -ArgumentList $appPath 
				#Invoke-Command -ScriptBlock { param($path) Start-Process -FilePath $path -ErrorAction SilentlyContinue } -ArgumentList $vsCodePath
            #}
            "*Obsidian*" {
                Start-Job -ScriptBlock { param($path) Start-Process -FilePath $path } -ArgumentList $appPath
            }
            "*Tailscale*" {
                Start-Job -ScriptBlock { param($path) Start-Process -FilePath $path } -ArgumentList $appPath
            }
            default {
                Start-Process $appPath
            }
        }
    } else {
        Write-Host "The application $appPath is already running and does not need to be opened again."
    }
}


$shortcutPathVscode = "D:\desktop\Visual Studio Code.lnk"
Start-Job -ScriptBlock {
    param($pathcode)
    Start-Process -FilePath $pathcode
} -ArgumentList $shortcutPathVscode
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.