0
votes

I am trying to pass folder path to Compress Archive function through clipboard but it shows me error. I am using following code:

$toZipDirectory = 'C:\Users\sadam.shaikh\Desktop\Automate\'

$zipFilePath = 'C:\Users\sadam.shaikh\Desktop\Automate\zip.zip'

$Newness= Get-ChildItem -Path $toZipDirectory | Where-Object {$_.PSIsContainer} |

Sort-Object LastWriteTime -Descending | Select-Object -First 1 | % { $_.FullName } | Clip

$directoryname= Get-Clipboard

Compress-Archive -Path $directoryname -DestinationPath $zipFilePath

Error: Compress-Archive : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At line:6 char:24 Compress-Archive -Path $directoryname -DestinationPath $zipFilePath CategoryInfo : InvalidData: (:) [Compress-Archive], ParameterBindingValidationException FullyQualifiedErrorId : ParameterArgumentValidationError,Compress-Archive

1
why you dont do it: Compress-Archive -Path $toZipDirectory -DestinationPath $zipFilePath ? - Esperento57
Because I want gci to search for last write folder and compress that one. - Sadam Hussain Shaikh

1 Answers

0
votes

IF you want zip the last created folder in $toZipDirectory:

$toZipDirectory = 'C:\Users\sadam.shaikh\Desktop\Automate'
$zipFilePath = 'C:\Users\sadam.shaikh\Desktop\Automate\zip.zip'

Get-ChildItem $toZipDirectory -directory | 
    sort CreationTime -Descending | select -First 1 | 
         Compress-Archive -DestinationPath $zipFilePath