How to find the working directory for hosted and self-hosted agent

GABRIEL OKOM 20 Reputation points
2025-03-16T19:17:40.3766667+00:00

I am working on a windows agent and having some issues finding the path.

Where do I find the working directory for both self-hosted and hosted agent?

And please, how can I view the content of the path?

I do know that for a self-hosted agent I can login to the VM and view the path, however; I am not sure how I can view the path for a hosted agent.

Azure DevOps
0 comments No comments
{count} votes

Accepted answer
  1. Damilola Onadeinde 475 Reputation points
    2025-03-16T20:00:37.1233333+00:00

    The work directory is typically located:

    • Self-hosted agent:
      • C:\agent\_work on Windows
      • ~/agent/work on macOS
      • /home/agent/_work on Linux.

    You can use File Explorer or command-line tools like dir or Get-ChildItem to view the contents of the _work directory:

    dir C:\agents\<agent-name>\_workor in PowerShell:
    
    Get-ChildItem C:\agents\<agent-name>\_work
    
    
    • Microsoft-hosted agent:
      • C:\a on Windows,
      • /Users/runner/work on macOS
      • /home/vsts/work on Linux.

    You can use pipeline tasks to list the contents of the working directory:

    • For Windows:
        - script: dir %AGENT_WORKFOLDER%
          displayName: 'List Working Directory Contents'For Linux:
      
        - script: ls $AGENT_WORKFOLDER
          displayName: 'List Working Directory Contents'
      

    The work directory structure is as follows:

        - /work directory
            - /1 build directory/pipeline workspace
                - /s source/working directory
                - /b binaries directory
                - /a artifacts staging directory
                - /TestResults Test results directory
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Marcin Policht 49,640 Reputation points MVP Volunteer Moderator
    2025-03-16T19:38:08.8933333+00:00

    The working directory for agents varies depending on whether you're using a self-hosted agent or a Microsoft-hosted agent.

    1. Finding the Working Directory

    • Self-hosted agent:
      • The working directory is typically under:
            C:\agent\_work
        
      • You can also find it by logging into the agent VM and checking the workFolder setting in the C:\agent\config.cmd file.
    • Microsoft-hosted agent:
      • The working directory is dynamically assigned for each job under:
            C:\Users\VssAdministrator\AppData\Local\Temp
        
      • The build directory is:
            D:\a\1\
        
      • You can determine the exact path dynamically using:
            - script: echo $(Agent.BuildDirectory)
              displayName: 'Show Working Directory'
        
        or
            - script: dir $(Build.SourcesDirectory)
              displayName: 'List Source Directory'
        

    2. Viewing the Path Content

    • Self-hosted Agent:
      • Log in to the VM and manually browse the C:\agent\_work folder.
      • Use PowerShell or Command Prompt to list the files:
            Get-ChildItem -Path C:\agent\_work -Recurse
        
    • Microsoft-hosted Agent:
      • Add a debug task in your pipeline to print the directory content:
            - script: dir /s $(Agent.BuildDirectory)
              displayName: 'List Working Directory'
        
      • If you need more details, you can use PowerShell:
            - powershell: Get-ChildItem -Path $(Build.SourcesDirectory) -Recurse
              displayName: 'List Source Directory'
        

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.