locked
How to code this powershell? RRS feed

  • Question

  • I would like to know on how to code powershell based on following condition

    1) Check on whether file "C:\Users\XXX\Downloads\ABC.xlsx" is existed or not

    2) If yes, copy this file to C:\Users\XXX\Downloads\DEF.xlsx and overwrite any existing file with same username

    3) Delete C:\Users\XXX\Downloads\ABC.xlsx

    If (Test-Path C:\User\XXX\Downloads\ABC.xlsx) {
    Copy-Item -Path C:\User\XXX\Downloads\ABC.xlsx -Destination C:\User\XXX\Downloads\DEF.xlsx
    }

    Above coding seems not working.

    Does anyone have any suggestions on what wrong it is?
    Thanks in advance for any suggestions


    Thanks in advance for any suggestions


    Wednesday, July 4, 2018 2:29 PM

Answers

  • $path = "C:\Users\request_479.xml"
    $DestinationPath = "D:\"
    
    if ([System.IO.File]::Exists($path))
    {
        Copy-Item  -Path $path -Destination $DestinationPath -Force
        Remove-Item -Path $path -Force
    }
    
    Best regards.
    • Marked as answer by oemMicrosoft Wednesday, July 4, 2018 2:59 PM
    Wednesday, July 4, 2018 2:54 PM

All replies

  • This forum doesn't write scripts for you, this is a fairly simple script to make. What have you tried so far and what errors are you getting?

    If you are really stuck then I would recommend looking at the "Test-Path" commandlet or looking into robocopy

    Wednesday, July 4, 2018 2:47 PM
  • We do not write Pwershell scripts on reuquest. If you like to do it by yourself you should start with the cmdlets Test-Path, Copy-Item and Remove-Item.

    If you'r ea Powershell beginner you might start with learning the very basics of Powershell from scratch with this free and entertaining video course at the Mictrosoft Virtual Academiy - Getting Started with Powershell.

    Have fun and a lot of success!


    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, July 4, 2018 2:50 PM
  • $path = "C:\Users\request_479.xml"
    $DestinationPath = "D:\"
    
    if ([System.IO.File]::Exists($path))
    {
        Copy-Item  -Path $path -Destination $DestinationPath -Force
        Remove-Item -Path $path -Force
    }
    
    Best regards.
    • Marked as answer by oemMicrosoft Wednesday, July 4, 2018 2:59 PM
    Wednesday, July 4, 2018 2:54 PM
  • Thanks, to everyone very much for suggestions (^v^)

    Thanks in advance for any suggestions

    Wednesday, July 4, 2018 3:00 PM
  • This is cleaner:

    $path = test-path "PATH"
    
    if ($path){
     #file is found
    }else{
     #file isn't found
    }

    Wednesday, July 4, 2018 3:00 PM
  • $path = "C:\Users\request_479.xml"
    $DestinationPath = "D:\"
    
    if ([System.IO.File]::Exists($path))
    {
        Copy-Item  -Path $path -Destination $DestinationPath -Force
        Remove-Item -Path $path -Force
    }
    Best regards.
    Really great and really helpful. Thanks a lot.

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, July 4, 2018 3:12 PM