How to load PSADT v4 variables for Invoke-AppDeployToolkit.ps1

Hi
I was wondering how I can access and test something with PSADT v4 variables

I was reading different answers just like this: ($dirSupportFiles in PSADT 4)

But the answers that are given does somehow not working for me

I have tried to run this let say in ISE:
I run the whole script which says it need to be an admin which I think is fine, and I thought the script is loaded
then i run this:

$adtSession = Open-ADTSession -SessionState $ExecutionContext.SessionState -PassThru
it says again I need to be an admin, ok I re-open it as admin:

$adtSession = Open-ADTSession -SessionState $ExecutionContext.SessionState -PassThru
Open-ADTSession : The term 'Open-ADTSession' is not recognized as the name of a cmdlet, function, script file, 
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:15
+ $adtSession = Open-ADTSession -SessionState $ExecutionContext.Session ...
+               ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Open-ADTSession:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CommandNotFoundException

What am I doin wrong?

Hi,

This is what I do when I want to test some of the PSADT functions or variables:

  1. Copy PSADT to C:\Temp\PSADT on my virtual machine used for testing
  2. Open PowerShell ISE as administrator and type: Import-Module C:\Temp\PSADT\PSAppDeployToolkit\PSAppDeployToolkit.psd1
  3. Type: Initialize-ADTModule
  4. Type: $adtSession = Open-ADTSession -SessionState $ExecutionContext.SessionState -ScriptDirectory "C:\Temp\PSADT" -PassThru
  5. Type: Export-ADTEnvironmentTableToSessionState -SessionState $ExecutionContext.SessionState

Now you can test the PSADT functions and variables, like:

Copy-ADTFile -Path "$($adtSession.DirSupportFiles)\Config.ini" -Destination "$($envAllUsersProfile)\Liquidware"

Variables like $adtSession.DirSupportFiles will then be resolved to C:\Temp\PSADT\SupportFiles

PSADT v4’s front script (Invoke-AppDeployToolkit.ps1) does not run linear like PSADT v3.
It’s chopped up into functions and then calls those functions near the end of the script.

In your case, you tried to use Open-ADTSession before the PSADT module was imported.
So PowerShell doesn’t know what Open-ADTSession is and gave you that error.

To see what’s in $adtSession you will need to do that AFTER the session is open.

In the Install-ADTDeployment Function, you can add $adtSession on a blank line and it will show what’s inside $adtSession in the PowerShell console.

Thank you for your reply and explanations!
When i run that lines, I see the variables are not populated still.
So, what is going wrong here, why I still cannot access variables and test the function? I appreciate your response!

Import-Module ".\PSAppDeployToolkit\PSAppDeployToolkit.psd1"    
Initialize-ADTModule
$adtSession = Open-ADTSession -SessionState $ExecutionContext.SessionState -ScriptDirectory ".\" -PassThru
Export-ADTEnvironmentTableToSessionState -SessionState $ExecutionContext.SessionState

Copy-ADTFile -Path "$($adtSession.DirSupportFiles)\policies.json" -Destination "$($envAllUsersProfile)\Test"
[2025-07-24 10:56:10.286] [Execution] [Copy-ADTFile] [Error] :: Failed to copy file(s) in path [\policies.json] to destination [C:\ProgramData\Test].
Error Record:
Message               : Cannot find path 'C:\policies.json' because it does not exist.
                        
FullyQualifiedErrorId : PathNotFound,Copy-ADTFile
ScriptStackTrace      : at Copy-ADTFile<Process>, C:\Users\ruf\OneDrive - Rejsekort og Rejseplan A S\Intune\Input\Mozilla Firefox vLatestVersion (64-bit) EN\PSAppDeployToolkit\PSAppDeployToolkit.psm1: line 6014
                        at <ScriptBlock>, <No file>: line 1
                        
PositionMessage       : At line:1 char:1
                        + Copy-ADTFile -Path "$($adtSession.DirSupportFiles)\policies.json" -De ...
                        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[2025-07-24 10:56:10.299] [Execution] [Copy-ADTFile] [Info] :: ContinueFileCopyOnError not specified, exiting function.

if I run the $adtSession then I see this:

DeploymentType              : Install
DeploymentTypeName          : Installation
DeployMode                  : Interactive
AllowRebootPassThru         : False
TerminalServerMode          : False
DisableLogging              : False
AppVendor                   : 
AppName                     : PSAppDeployToolkit
AppVersion                  : 4.0.6
AppArch                     : 
AppLang                     : DA
AppRevision                 : 01
AppSuccessExitCodes         : {0}
AppRebootExitCodes          : {1641, 3010}
AppScriptVersion            : 
AppScriptDate               : 
AppScriptAuthor             : 
InstallName                 : PSAppDeployToolkit_4.0.6_DA_01
InstallTitle                : PSAppDeployToolkit 4.0.6
DeployAppScriptFriendlyName : 
DeployAppScriptVersion      : 
DeployAppScriptParameters   : 
InstallPhase                : Execution
CurrentDate                 : 24-07-2025
CurrentTime                 : 10:55:45
ScriptDirectory             : {.\}
DirFiles                    : 
DirSupportFiles             : 
DefaultMsiFile              : 
DefaultMstFile              : 
DefaultMspFiles             : {}
UseDefaultMsi               : False
LogTempFolder               : C:\Users\rufadmin\AppData\Local\Temp\PSAppDeployToolkit_4.0.6_DA_01_Install
LogName                     : PSAppDeployToolkit_4.0.6_DA_01_PSAppDeployToolkit_Install.log
CurrentDateTime             : 24-07-2025 10:55:45

DirFiles/DirSupportFiles will only be populated from the command line if you open a session from a directory that contains those folders. Right now in your current context, there probably is no such folders, therefore the values are null.

Also, no need for Export-ADTEnvironmentTableToSessionState -SessionState $ExecutionContext.SessionState after opening a new DeploymentSession. Open-ADTSession will handle this for you.