P$ script, copy file, move and rename, rename old file to include last months date

Marc74 0 Reputation points
2023-06-09T10:15:24.6633333+00:00

trying to work it out and not getting that far so far

i manually download a file every month.

this needs to be renamed to setup.exe and moved to a network drive. simples

in the network drive is already a file called setup.exe and this would need to be renamed to last months title Maysetup.exe

after a few months i would delete some of the files and avoid any issues at the end of the year etc

getting a little stumped though

any advice please?

Small BASIC
Small BASIC
A programming language created by Microsoft that serves a stepping stone for beginners from block-based coding languages to more complex text-based languages.
280 questions
Windows Network
Windows Network
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.Network: A group of devices that communicate either wirelessly or via a physical connection.
755 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,509 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Rafael da Rocha 5,166 Reputation points
    2023-06-09T10:44:49.78+00:00

    Hello Marc,

    this should be OK as a start for what you're looking for, just adapt it to your use case

    $RenPat = (Get-Culture).DateTimeFormat.GetAbbreviatedMonthName((Get-Date).AddMonths(-1).Month)
    
    if (Test-Path 'c:\temp\setup.exe') {
        Rename-Item 'c:\temp\setup.exe' -NewName $RenPat"_setup.exe"
    }
    
    Invoke-WebRequest -Uri 'https://github.com/notepad-plus-plus/notepad-plus-plus/releases/download/v8.5.3/npp.8.5.3.Installer.x64.exe' -OutFile 'c:\temp\setup.exe'
    
    
    Get-ChildItem -Path 'c:\temp\' | Where-Object { ($_.Name -like '*_Setup.exe') -and ($_.LastWriteTime -lt (Get-Date).AddMonths(-6)) } | Remove-Item
    
    0 comments No comments

  2. Marc74 0 Reputation points
    2023-06-09T11:02:41.6533333+00:00

    looks like agood start, ill have a look after lunch ty

    to confirm i will manually download the file as name will change and place in a folder.

    then run a script to copy that file to my network drive and place in folder renamed as setup.exe

    the file already on my network drive called setup.exe will just need to be renamed to lastmonthsetup.exe and left in location


  3. Marc74 0 Reputation points
    2023-06-09T13:03:29.6266667+00:00

    ok this works for renaming my local file to may_setup.exe

    but i need my local file to copy to the network share after it has renamed the current setup.exe on network to may_setup.exe

    0 comments No comments

  4. Marc74 0 Reputation points
    2023-06-09T13:13:03.5333333+00:00

    ok so edited your script to just rename my network share, works good.

    now presue just add my copy command after to copy my local to destination then should be good

    0 comments No comments

  5. Marc74 0 Reputation points
    2023-06-09T13:19:56.8966667+00:00

    thanks m8, i think thsi is it now

    $RenPat = (Get-Culture).DateTimeFormat.GetAbbreviatedMonthName((Get-Date).AddMonths(-1).Month)
    
    if (Test-Path '\\server\name$\receive\setup.exe') {
        Rename-Item '\\server\name$\receive\setup.exe' -NewName $RenPat"_setup.exe"
    }
    
    
    Get-ChildItem -Path 'c:\temp\' | Where-Object { ($_.Name -like '*_Setup.exe') -and ($_.LastWriteTime -lt (Get-Date).AddMonths(-6)) } | Remove-Item
    
    Copy-Item -Path "C:\temp\files2\*" -Destination "\\server\name$\receive\setup.exe"
    

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.