troubleshooting Question

Powershell script fails with error code (0x1)

Avatar of rwheeler23
rwheeler23Flag for United States of America asked on
Powershell.NET Programming
10 Comments1 Solution26 ViewsLast Modified:

I have this Powershell script that uses WinSCP to upload a file. This code is sample from their documentation. I have supplied the sessions options but when I run this script it returns error code (0x1). I am running this script with this command.

powershell.exe -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "C:\GPShares\Automation\Powershell\HMSVAutoXfer.ps1"


# HMSVAutoXfer.ps1 - transfer most recent file to server

    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = ""
        UserName = ""
        Password = ""
        SshHostKeyFingerprint = ""
    $session = New-Object WinSCP.Session
        # Connect
        $localPath = "C:\GPShares\ImportFiles\SOPExport"
        $remotePath = "/upload"
        # Select the most recent file.
        # The !$_.PsIsContainer test excludes subdirectories.
        # With PowerShell 3.0, you can replace this with -File switch of Get-ChildItem.
        $latest =
            Get-ChildItem -Path $localPath |
            Where-Object {!$_.PsIsContainer} |
            Sort-Object LastWriteTime -Descending |
            Select-Object -First 1
        # Any file at all?
        if ($latest -eq $Null)
            Write-Host "No file found"
            exit 1
        # Upload the selected file
            [WinSCP.RemotePath]::Combine($remotePath, "*")).Check()
        # Disconnect, clean up
    exit 0
    Write-Host "Error: $($_.Exception.Message)"
    exit 1

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 10 Comments.
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>


Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 10 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004