Home > database >  PowerShell unable to compress and delete source files in my desktop?
PowerShell unable to compress and delete source files in my desktop?

Time:09-05

I need some help in fixing the issue with the PowerShell command to compress and delete the .CSV files from my desktop.

The error is as follows:

At line:14 char:5
      Compress-Archive @paramCompressArchive
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidArgument: (Get-ChildItem -...ldersStats.CSV":String) [Compress-Archive], InvalidOperationException
      FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Compress-Archive

This is my script so far, which is part of my larger script, but I just need to fix the issue on the moving to ZIP archive and then delete section:

$MailboxName = '[email protected]'
try {
    $paramCompressArchive = @{
        Path            = 'Get-ChildItem -Path "$([Environment]::GetFolderPath("Desktop"))\" 
                                         -Include "$($MailboxName)-RetentionPolicyTags.CSV", 
                                                  "$($MailboxName)-Details.CSV", 
                                                  "$($MailboxName)-Diagnostic.CSV", 
                                                  "$($MailboxName)-MRMLogs.CSV",
                                                  "$($MailboxName)-MbxStats.CSV",
                                                  "$($MailboxName)-MbxFoldersStats.CSV",
                                                  "$($MailboxName)-ArchiveMbxFoldersStats.CSV"'
        DestinationPath = "$([Environment]::GetFolderPath("Desktop"))\$($MailboxName).ZIP"
        Force           = $true
    }
   
    Compress-Archive @paramCompressArchive
    
    $paramRemoveItem = @{
        Path    = "$([Environment]::GetFolderPath("Desktop"))\"
        Include = "$($MailboxName)-RetentionPolicyTags.CSV", 
                    "$($MailboxName)-Details.CSV", 
                    "$($MailboxName)-Diagnostic.CSV", 
                    "$($MailboxName)-MRMLogs.CSV",
                    "$($MailboxName)-MbxStats.CSV",
                    "$($MailboxName)-MbxFoldersStats.CSV",
                    "$($MailboxName)-ArchiveMbxFoldersStats.CSV"
        Force   = $true
        WhatIf  = $true
    }
   
    Remove-Item @paramRemoveItem
}
catch {
    $error[0].exception.gettype().fullname
}

CodePudding user response:

Continuing from my comment, you can create variables only once in your code like and use these like:

$MailboxName = '[email protected]'
$Path        = [Environment]::GetFolderPath("Desktop")
$Include     = 'RetentionPolicyTags', 'Details', 'Diagnostic', 'MRMLogs',
               'MbxStats', 'MbxFoldersStats', 'ArchiveMbxFoldersStats' | ForEach-Object {
                   '{0}-{1}.csv' -f $MailboxName, $_
               }

# get the list of files to process
$files = (Get-ChildItem -Path $Path -Filter '*.csv' -File -Recurse -Include $Include).FullName
# create a Hashtable for splatting (used for both Compress-Archive and Remove-Item)
$paramCompressAndDelete = @{
    Path            = $files
    DestinationPath = Join-Path -Path $Path -ChildPath ('{0}.ZIP' -f $MailboxName)
    Force           = $true
}

try {
    Compress-Archive @paramCompressAndDelete
    # now use the same splatting Hashtable for deletion
    $paramCompressAndDelete.Remove('DestinationPath')
    Remove-Item @paramCompressAndDelete -WhatIf
}
catch {
    throw
}

CodePudding user response:

The error is because of the quoting around the command that you want to use to get the paths of the files to compress:

 Path            = 'Get-ChildItem -Path  ... '

Just remove the quotes and also make sure that you extract the full path (.Fullname) from the Get-ChildItem output.

Also, for -Include to work, the -Path argument must end with a wildcard (*).

To remove code duplication, define the include list as an array at the beginning, so you can use it for both Compress-Archive and Remove-Item. Further code duplication can be avoided by storing the result of [Environment]::GetFolderPath() into a variable.

$includeList = @(
    "$($MailboxName)-RetentionPolicyTags.CSV"
    "$($MailboxName)-Details.CSV"
    "$($MailboxName)-Diagnostic.CSV"
    "$($MailboxName)-MRMLogs.CSV"
    "$($MailboxName)-MbxStats.CSV"
    "$($MailboxName)-MbxFoldersStats.CSV"
    "$($MailboxName)-ArchiveMbxFoldersStats.CSV"
)

$desktopDir = [Environment]::GetFolderPath('Desktop')
$desktopDirWildCard = Join-Path $desktopDir *

$items = Get-ChildItem -Path $desktopDirWildCard -Include $includeList

$paramCompressArchive = @{
    Path            = $items.Fullname   # Creates array of full paths
    DestinationPath = Join-Path $desktopDir "$MailboxName.ZIP"
    Force           = $true
}

Compress-Archive @paramCompressArchive
    
$paramRemoveItem = @{
    Path    = $desktopDirWildCard
    Include = $includeList
    Force   = $true
    WhatIf  = $true
}
   
Remove-Item @paramRemoveItem
  • Related