Skip to content

Instantly share code, notes, and snippets.

@claudenobs
Last active December 2, 2018 14:13
Show Gist options
  • Save claudenobs/f0e14a3d38ed1c3ba36b to your computer and use it in GitHub Desktop.
Save claudenobs/f0e14a3d38ed1c3ba36b to your computer and use it in GitHub Desktop.
Creates shortcuts to all your steam games. Icons are taken either from steam installation downloaded or game executable.
# 0. Requires steamcmd. Download from https://developer.valvesoftware.com/wiki/SteamCMD. Put it in your steam directory (the one with steam.exe)
# 1. Save this script by right-click on Raw button and choose "Save link as..."
# 2. Start powershell : right click "Start" then choose "Run..." & type : powershell
# 3. Run script type : Start-Process powershell -Verb RunAs -ArgumentList "-ExecutionPolicy Unrestricted -NoExit -file `"$Home\Downloads\SteamShortcutCreator.ps1`""; exit
# 4. Give permission in pop-up
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$steamDir = if (Test-Path "C:\Program Files (x86)\Steam\steam.exe") { "C:\Program Files (x86)\Steam" }
elseif ($args[0] -and (Test-Path "$($args[0])\steam.exe")) { $args[0] }
elseif (Test-Path "$PSScriptRoot\steam.exe") { $PSScriptRoot }
elseif (Test-Path ".\steam.exe") { "." }
else { Write-Host "Steam not found. Please pass correct path as argument"; return }
$shortcutDir = "$Home\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Steam"
Write-Host "Found steam installation at $steamDir"
Write-Host "Will install shortcuts to $shortcutDir"
function steamCmd([array]$arguments) {
$cmd = (Get-Item "$steamDir\steamcmd.exe").FullName
# Write-Host "Executing: $cmd $arguments"
for ($i=0; $i -lt 50; $i++){ $arguments+="+app_info_print 0" } # forcing the pipe to flush by spamming some useless output
$arguments += "+quit"
[string]$out= & $cmd $arguments
$out.subString(0, $out.IndexOf("AppID : 0,")) # remove useless output
}
function createDirectory($dir) {
if (-not (Test-Path $dir)) {
mkdir $dir
Write-Host "Created directory: $dir"
}
}
function getSteamInstalledAppList {
(steamCmd "+apps_installed" | Select-String "AppID (\d*) : `"([^`"]*)`" : (.*?)\s+(?=AppID)" -AllMatches).Matches | foreach {
@{"AppID" = $_.Groups[1].value; "Name"=$_.Groups[2].value.replace(":",""); "Path" = $_.Groups[3].value; "Icon" = ""}
}
}
function createShortcut($app) {
$shortcut = "$shortcutDir\$($app.Name).lnk"
if (Test-Path $shortcut) {
Write-Host "skipping $shortcut (file already exists)"
} else {
$shell = New-Object -comObject WScript.Shell
$lnk = $shell.CreateShortcut($shortcut)
$lnk.TargetPath = "$steamDir\steam.exe"
$lnk.Arguments = "-applaunch $($app.AppID)"
$lnk.IconLocation = $app.Icon # if () { $app.Icon } else { $app.Path }
$lnk.Save()
Write-Host "creating shortcut $shortcut"
}
}
function enrichWithDetails($apps) {
for($i=0; $i -lt $apps.length; $i++) {
$app = $apps[$i]
$out = steamCmd "+app_info_print $($app.AppID)"
if ($out -match "`"clienticon`"") {
$uuid = ($out | Select-String "`"clienticon`"\s*`"([^`"]*)`"" -AllMatches).Matches.Groups[1].value
$app.Icon = "$steamDir\steam\games\$uuid.ico"
}
if (-not ($app.Icon -and (Test-Path $app.Icon)) -and $out -match "`"icon`"") {
$uuid = ($out | Select-String "`"icon`"\s*`"([^`"]*)`"" -AllMatches).Matches.Groups[1].value
if ($uuid) {
$app.Icon = downloadJpgAsIcon $app $uuid
} else {
$exe = ($out | Select-String "`"executable`"\s*`"([^`"]*)`"" -AllMatches).Matches.Groups[1].value
$app.Icon = "$($app.Path)\$exe"
}
}
Write-Host "." -nonewline
}
Write-Host ""
}
function downloadJpgAsIcon($app, $uuid) {
$url = "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/$($app.AppID)/$uuid.jpg"
$output = "$steamDir\steam\games\$uuid).ico"
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$jpg = [System.Drawing.Bitmap]::FromStream([System.Net.HttpWebRequest]::Create((New-Object System.Uri($url))).GetResponse().GetResponseStream())
$stream = [System.IO.File]::OpenWrite($output)
[System.Drawing.Icon]::FromHandle($jpg.GetHicon()).save($stream)
$stream.close()
$output
}
$installed = getSteamInstalledAppList
Write-Host "$($installed.Count) steam games found."
enrichWithDetails $installed
createDirectory $shortcutDir
foreach ($app in $installed) { createShortcut $app }
@SGHphoto
Copy link

SGHphoto commented Dec 2, 2018

My games are installed in a library on another drive and scripting is something I'm not good at and in powershell i'm a noob (and barely that). Steam is installed in default place, Steam library is at d:\Steamlibrary. Is there a way to change this script to include steamlibraries in non default locations?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment