0

I wanted to zip the out put of

Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 

i have tried this

$command ='Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1';

$file= iex $command

and put $file in the zip function but resulted into error .

Get-ChildItem : Access to the path 'C:\Windows\System32\Com\dmp' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\Com\dmp:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\config' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\config:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\Configuration' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\Configuration:String) [Get-ChildItem], UnauthorizedAccessExcept 
   ion
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\ias' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\ias:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\inetsrv\config' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\inetsrv\config:String) [Get-ChildItem], UnauthorizedAccessExcep 
   tion
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\Fax\Incoming' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\Syst...es\Fax\Incoming:String) [Get-ChildItem], UnauthorizedAccessExcept 
   ion
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\Fax\Outgoing' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\Syst...es\Fax\Outgoing:String) [Get-ChildItem], UnauthorizedAccessExcept 
   ion
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\Firewall' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\LogFiles\Firewall:String) [Get-ChildItem], UnauthorizedAccessEx 
   ception
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\HTTPERR' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\LogFiles\HTTPERR:String) [Get-ChildItem], UnauthorizedAccessExc 
   eption
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\LogFiles\WMI' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\LogFiles\WMI:String) [Get-ChildItem], UnauthorizedAccessExcepti 
   on
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\MsDtc' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\MsDtc:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\networklist' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\networklist:String) [Get-ChildItem], UnauthorizedAccessExceptio 
   n
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\spool\PRINTERS' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\spool\PRINTERS:String) [Get-ChildItem], UnauthorizedAccessExcep 
   tion
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\spool\SERVERS' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\spool\SERVERS:String) [Get-ChildItem], UnauthorizedAccessExcept 
   ion
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\sru' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\sru:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\Tasks' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\Tasks:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\wbem\MOF' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\wbem\MOF:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\wdi' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\wdi:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Access to the path 'C:\Windows\System32\wfp' is denied.
At E:\scripts\last modified folder.ps1:15 char:8
+ $bak = Get-ChildItem -Recurse -Path $filePath #| Where-Object { $_.Extension -eq ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (C:\Windows\System32\wfp:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
NoDataFound
  • 11,381
  • 33
  • 59

1 Answers1

1

There is something missing from your code. As it stands this contains no information about zipping a directory. The root of the issue with the code you are showing is you are asking Invoke-Expression to run a folder name. Your $command is returning an object that contains one folder. Was that what you wanted?

You then take that folder object and put it into Invoke-Expression. $file = means that you mean to put the results of Invoke-Expression into $file. I also notice you have the command in single quotes which would mean Invoke-Expression would attempt to expand it. More testing is required. I would take a guess that you want to change this up a little.

$folder = Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName

That would at least return the full folder path that you are looking for. What you are trying to do with it is what i'm lost on. If you have at least DotNet 4.5 you could zip like this example from another SO question

$folder = Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName
$destination = "e:\test\myZip.zip"
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
[System.IO.Compression.ZipFile]::CreateFromDirectory($folder, $destination)

With PowerShell Community Extensions you will also find a cmdlet called Write-Zip which you could also use

Get-ChildItem -Path E:\test | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 | Write-Zip -IncludeEmptyDirectories -OutputPath E:\Test\MyZip.zip

If you would prefer: Here is a solution that just uses functionality builtin to PowerShell Creating a zipped/compressed folder in Windows using Powershell or the command line

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119