Powershell use Invoke-command to check a file existence on remote server and out-file to a txt file on shared folder failed

Anson R C XUE 1 Reputation point

Hi there.
I have a script to check a file existence on remote server and out-file to result to a txt file on shared folder, but it failed with access denied error.

Foreach ($name in $serverlist)
$Session = New-PSSession -ComputerName $name
Invoke-Command -Session $Session -ScriptBlock {
if ((Test-Path "c:\test.txt") -eq "True")
Write-Output $name "file exists" >> "\server\shared folder\result.txt"
else {
Write-Output $name "file not found" >> "\server\shared folder\result.txt"

The error is "Access to the path \server\shared folder\result.txt" is denied. however i have verified the permission, the user which runs this script was granted full permission on the shared folder.

Can anyone help to advise what's going wrong?


Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,407 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 31,171 Reputation points Microsoft Vendor


    You have to enable the second hop.

    This link could be helpful.
    Enable PowerShell "Second-Hop" Functionality with CredSSP

    Best Regards,
    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.