Table of Contents

Example scripts


Below are some examples of useful scripts you can use:

Starting additional application(s) before game starts and killing it after game exits

  • Set first script to start your application using Start-Process cmdlet to the
Start-Process -FilePath "c:\somepath\someapp.exe" -ArgumentList "-some arguments"

If you want to start the application minimized and it doesn't have native support for it, you can add the -WindowStyle Minimized argument to the end of the line.

Warning

Some applications won't work properly (or even start) when started using working directory outside of their application directory. In that case you need to use -WorkingDirectory parameter and specify working directory manually.

  • Set second script to kill the application using Stop-Process cmdlet
Stop-Process -Name "someapp"
  • If an application requires elevated rights to start, then you need to use different method to stop its running process.
(Get-WmiObject -Class Win32_Process -Filter "name = 'someapp.exe'").Terminate()
Note

To identify the name of the process that should be used in your scripts to stop an application, you can follow these steps: Open Windows Task Manager by pressing Ctrl+Shift+Esc or by right-clicking on the taskbar and selecting "Task Manager." Then in Task Manager, right-click on any column header and choose the "Process name" option to enable it.

Starting application only if it's not running

Detecting if an application is running can be done by using the Get-Process cmdlet and then combined with the explanation in the previous example script

if (!(Get-Process -Name "someapp" -EA 0))
{
    # Process is not running. Combine with previous section
}
else
{
    # Process is running. Combine with previous section
}

Simulate key presses

It's possible to do with the SendKeys class.

# Load the necessary assembly
Add-Type -AssemblyName System.Windows.Forms

# List of keys codes available at https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys?view=windowsdesktop-6.0#remarks
# Simulate F2 key press.
[System.Windows.Forms.SendKeys]::SendWait('{F2}')

# Simulate Ctrl+D Combo key press.
[System.Windows.Forms.SendKeys]::SendWait('^(D)')

# Simulate Ctrl+Shift+Escape Combo key press.
[System.Windows.Forms.SendKeys]::SendWait('^(+{ESC})')

Executing actions depending of game characteristics

It's possible to only execute scripts on games that that have certain characteristics, for example:

if ($game.Features.Name -contains "Some feature name")
{
    # Game has the feature!
}

if ($game.Source.Name -eq "Steam")
{
    # Game is a Steam game!
}

Execute actions depending on active Playnite mode

If you want to adjust the script's behavior based on the active Playnite's operation mode (Desktop vs Fullscreen), you can get that information from the API object:

if ($PlayniteApi.ApplicationInfo.Mode -eq "Desktop")
{
    # Execute when running in Desktop mode
}
else
{
    # Execute when running in Fullscreen mode
}