Powershell GPO Backup – want to delete old backup

Welcome Forums General PowerShell Q&A Powershell GPO Backup – want to delete old backup

Viewing 2 reply threads
  • Author
    Posts
    • #161697
      Participant
      Topics: 4
      Replies: 5
      Points: 49
      Rank: Member

      Hi Powershell Folks

      I have a good working Powershell script that takes GPO backup

      I want the backup will remove the old folder and put a new folder,

      like that 😉

      **************************************** REMOVE OLD FOLDER*****

      ##Check for an existing backup at Target Network Location
      $oldbackup = Test-Path -Path "\\SERVER\GPO_Backup\GPO\"
      if ($oldbackup){
      remove-item -Path "\\SERVER\GPO_Backup\GPO\" -Recurse -Force
      }
      
      *************************************************************************GPO BACKUP****
      
      Import-Module grouppolicy
      $date = get-date -format yyyy-MM-d
      New-Item -Path \\SERVER\GPO_Backup\GPO\$date -ItemType directory
      Backup-Gpo -All -Path \\SERVER\GPO_Backup\GPO\$date
      

      **********************

      Please help me

      Sokoban

    • #163338
      Participant
      Topics: 0
      Replies: 45
      Points: 243
      Helping Hand
      Rank: Participant

      @Sokoban you can try below Style as well 🙂

      Function Remove-backup {
      if(Test-Path -path "\\SERVER\GPO_Backup\GPO"){
      Remove-Item -path "\\SERVER\GPO_Backup\GPO\" -ErrorAction stop 
      }
      else{
      Write-Error "Share Path Is not Found" -ErrorAction continue
      }
      try{ 
      New-Item -Path "\\SERVER\GPO_Backup\GPO\$($(Get-date).tostring('yyyy-MM-d'))" -ItemType directory -ErrorAction Stop
      $newfolder=$($(Get-date).tostring('yyyy-MM-d')) 
      }
      catch{
      Write-Error "[Error:]$error[0].Exception.Message"
      
      }
      Return $newfolder
      }
      
      Import-Module grouppolicy
      $removebackup=Remove-backup
      Backup-Gpo -All -Path "\\SERVER\GPO_Backup\GPO\$removebackup"

       

      Thanks,
      Nitesh

    • #163341
      Participant
      Topics: 0
      Replies: 45
      Points: 243
      Helping Hand
      Rank: Participant

      You can do like this as well

      Function Remove-backup {
      if(Test-Path -path "\\SERVER\GPO_Backup\GPO"){
      Remove-Item -path "\\SERVER\GPO_Backup\GPO\" -ErrorAction stop 
      }
      else{
      Write-Error "Share Path Is not Found" -ErrorAction continue
      }
      try{ 
      New-Item -Path "\\SERVER\GPO_Backup\GPO\$($(Get-date).tostring('yyyy-MM-d'))" -ItemType directory -ErrorAction Stop
      $newfolder=$($(Get-date).tostring('yyyy-MM-d')) 
      }
      catch{
      Write-Error "[Error:]$error[0].Exception.Message"
      
      }
      Return $newfolder
      }
      
      Import-Module grouppolicy
      $removebackup=Remove-backup
      Backup-Gpo -All -Path "\\SERVER\GPO_Backup\GPO\$removebackup"
Viewing 2 reply threads
  • The topic ‘Powershell GPO Backup – want to delete old backup’ is closed to new replies.