Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 477357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:34:58+00:00 2026-05-13T00:34:58+00:00

I have a small script that copies files to a list of remote machines.

  • 0

I have a small script that copies files to a list of remote machines. In this script I use:

Copy-Item "$AppLocation\$AppName" -destination "\\$MachineName\c$\" -force

This can throw various types of errors. If this throws an error, I want to log the error to a file and then continue. My question is that I’d like to know what would be the right way to find out if the Copy-Item command was successful.

Next question is related:

psexec \\$MachineName -u $RemoteLogin -p $Remotepassword -s -i -d C:\$AppName

What would be a good way to find out how this command executed? I get a message in the console that it exited with 0 but I have no idea how I could get the return code into a local variable.

I can also use this:
(Get-WMIObject -ComputerName $MachineName -List | Where-Object -FilterScript {$_.Name -eq "Win32_Product"}).Install("C:\$AppName","","false")
which works fine, but still, no idea how to find out if it succeeded unless I read the output.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:34:58+00:00Added an answer on May 13, 2026 at 12:34 am

    Concerning the first part of your question, you can use

    Copy-Item ... -ErrorAction Continue -ErrorVariable yourVariable
    

    Error action tells the cmdlet what to do if case of error , ErrorVariable will put any error in the variable of your choice ($yourVariable in the exemple).

    $? automatic variable contains the execution status of the last operation. If it’s false, just check the content of $yourVariable to know what went wrong and do whatever you want with it (like write it in a file in your case).

    You can also use

    Copy-Item ... -ErrorAction Continue 2> [pathToLogFile]
    

    This will redirect the error stream of the cmdlet to a file of your choice…

    Concerning the second part of your question : $LASTEXITCODE contains the return code of your last executable.

    Have you considered using the remoting capabilities of Powershell 2 for more control ?

    Hope it helps

    Cédric

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this small PHP script that takes a POST variable and writes it
I have this small script that sorts the content of a text file #
I have a problem with my index.php, i have this small script that decides
I have this small script that shows a caption for a img when it
Currently I have this small script that makes it possible to check if a
I have a small script that checks a large list of domains for their
I have written a small batch script which copies files from the a folder
I have a small PHP script that takes the values from a dropdown list
I have a small script that I use to resize all of the images
I have a small script that use urllib2 to get contents of a site,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.