How to check for big files? - powershell

I tried to do like this but not works, any ideas?
if(Test-Path ~\desktop){
$bigfiles = Get-ChildItem ~\desktop -force -Include *.* | Where-Object {$_.Length -gt 10GB}
if{$bigfiles -gt 10GB){
echo "You have big files"
}
}
this is the error:
Cannot compare "C:\Users\mario\Desktop\AngelinaJolie_photo_in_4k.jpg" because it is not IComparable.
At line:7 char:8
+ if($bigfiles -gt 10GB){
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NotIcomparable

$bigfiles is $null if there are no results, a FileInfo if there is one result, otherwise it is an array of FileInfos. You can check for results using
if($bigfiles) { ... }

Related

import list to copy select files from large folder to a new folder in powershell

I am trying to move 34k images from a large dir that has 500k images. I only need the ones on the csv I created. the code I wrote call the item but doesn't move them. I wrote the code below to test on my desktop before touching production images (10 images but only want 5 to move). I am new to scripting, appologies for any mistakes I made. Thank you for any help that can be provided. I have learned a lot on this site.
Get-Content -path "C:\Users\waemisegger\Desktop\test1.csv" | ForEach-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports - Copy\images" -Recurse $_ -Destination "C:\Users\waemisegger\Desktop\test" }
this is the error I get:
PS C:\Windows\system32> Get-Content -path "C:\Users\waemisegger\Desktop\test1.csv" | ForEach-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports - Copy\images" -Recurse $_ -Destination "C:\Users\waemisegger\Desktop\test" }
Copy-Item : A positional parameter cannot be found that accepts argument 'FullName '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument '-------- '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-087.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-097.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-106lf.png'.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-118.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-159.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
here is what the csv looks like:
FullName
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-087.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-097.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-106lf.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-118.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-159.png
Edited to show before & after folder contents.
Assuming your CSV file looks like this:
Source
------
apple-ABC.doc
apple-ABC.pdf
Then this short script will do the work.
$MoveList = Import-Csv -path "G:\Test\FilesToMove.csv"
For ($Cntr = 0; $Cntr -lt $MoveList.Count; $Cntr++) {
$MIArgs =
#{Path = "G:\Test\$($MoveList[$($Cntr)].Source)"
Destination = "G:\Test\Music"}
Move-Item #MIArgs
}
Note if your CSV has the paths you just need to delete the test path [G:\Test] from the Path argument. Of course you'll provide appropriate paths for all items in my example.
Folders before running code:
Folders after running code:
HTH

Find & Replace text from multiple specific files from sub folders dynamically using powershell

I am working on azure CD pipeline, i want to change content of multiple files which exist in following folder structure.
MainFolder => SubFolder1 => myFile1.txt
SubFolder2 => myFile2.txt
SubFolder3 => myFile3.txt
I want to achieve my above requirement using powershell, and i have tried the following code.
$filepath = 'C:\Users\ashishjain06\Desktop\MainFolder'
$mydata = Get-ChildItem $filepath -include *.txt -recurse | Select-Object fullName
$totalRecords = $mydata.Count
for($x = 0; $x -lt $totalRecords; $x++)
{
((Get-Content -path $mydata[$x] -Force) -replace 'oldText','newText') | Set-Content -Path $mydata[$x]
}
When i run above code it's give me following output.
Get-Content : Cannot find drive. A drive with the name '#{FullName=C' does not exist.
At line:6 char:7
+ ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (#{FullName=C:String) [Get-Content], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:25
+ ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:25
+ ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:25
+ ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:25
+ ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
I am newbie on powershell, Please help me out to resolve this issue.
Get-ChildIItem returns (an array of) FileInfoObjects with a bunch of properties,
Select-Object drops all but the selected one, but it is still an object with one property FullName.
See $mydata | Get-Member
One method is Select-Object -ExpandProperty FullName,
another one is the property dereference operator . used in the following script.
To have the script work on any user don't include a fixed path, either use $Env:USERPROFILE or better yet, let the system evaluate current users Desktop folder (which might be relocated).
Instead of iterating the arrray $mydata by index let powershell do that with a foreach:
$filepath = Join-Path ([environment]::GetFolderPath('Desktop')) 'MainFolder'
$files = (Get-ChildItem $filepath -Filter *.txt -Recurse).FullName
foreach($file in $files){
(Get-Content -path $file -Force) -replace 'oldText','newText' | Set-Content -Path $file
}
It's still an object with a property unless you do this:
select-object -expandproperty fullname
Replace Select-Object with ForEach-Object, i.e.:
$filepath = 'C:\Users\ashishjain06\Desktop\MainFolder'
#$mydata = Get-ChildItem $filepath -include *.txt -recurse | Select-Object fullName
$mydata = Get-ChildItem $filepath -include *.txt -recurse | ForEach-Object fullName
$totalRecords = $mydata.Count
for($x = 0; $x -lt $totalRecords; $x++)
{
((Get-Content -path $mydata[$x] -Force) -replace 'oldText','newText') | Set-Content -Path $mydata[$x]
}

cannot rename because item does not exist

Trying to search a folder for files not already ending in *.txt which have not been modified in 1 day and rename the extension to .txt
$app_files = get-childitem "C:\Users\adm.aross\Desktop\Rename DHL files - TKT0087521\Test" -recurse -exclude *.txt | where-object {$_.LastWriteTime -lt (Get-Date).AddDays(-1)}
foreach ( $file in $app_files ) {
$newfile = $file.Name + ".txt"
Rename-Item -Literalpath "C:\Users\adm.aross\Desktop\Rename DHL files - TKT0087521\Test\$file" $newfile
}
Rename-Item : Cannot rename because item at
'C:\Users\adm.aross\Desktop\Rename DHL files -
TKT0087521\Test\C:\Users\adm.aross\Desktop\Rename DHL files -
TKT0087521\Test\01-06-2019.log' does not exist. At line:5 char:9
+ Rename-Item -Literalpath "C:\Users\adm.aross\Desktop\Rename D ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Rename-Item : Cannot rename because item at
'C:\Users\adm.aross\Desktop\Rename DHL files -
TKT0087521\Test\C:\Users\adm.aross\Desktop\Rename DHL files -
TKT0087521\Test\01-07-2019.log' does not exist. At line:5 char:9
+ Rename-Item -Literalpath "C:\Users\adm.aross\Desktop\Rename D ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Rename-Item : Cannot rename because item at
'C:\Users\adm.aross\Desktop\Rename DHL files -
TKT0087521\Test\C:\Users\adm.aross\Desktop\Rename DHL files -
TKT0087521\Test\08-05-2019.log.log' does not exist. At line:5 char:9
+ Rename-Item -Literalpath "C:\Users\adm.aross\Desktop\Rename D ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
$RootFolder = 'C:\Users\adm.aross\Desktop\Rename DHL files - TKT0087521\Test'
$CheckDate = [DateTime]::UtcNow.AddDays(-1)
Get-ChildItem -LiteralPath $RootFolder -Exclude #('*.txt') -Recurse:$true |
Where-Object { $_.psIsContainer -eq $false } | # Check is file, not a directory
Where-Object { $_.LastWriteTimeUtc -lt $CheckDate } |
Where-Object { $_.CreationTimeUtc -lt $CheckDate } |
Where-Object { ( Test-Path -LiteralPath "$($_.FullName).txt" -PathType Any ) -ne $true } | # Check if there is no .txt file already
ForEach-Object { Rename-Item -LiteralPath $_.FullName -NewName "$($_.Name).txt" }
Check WriteTime and CreationTime. There can be situations when file
is created AFTER written ( when file is copied from another source,
WriteTime is copied, and CreationTime is not )
Check you can rename the file (the target name not exist )
Check it is a file

Powershell script to search mutiple remote PC's for specific files

I am attempting to run a powershell script from my PC against a txt file that contains numerous remote PC's. I want to run a script against that list of PC's that will search a certain file path for specific file name and/or file extension.
I have been attempting to do a for each loop with my txt file inserted as a variable. I am also attempting to get the file names returned to me via the cmdlet "Get-ChildItem."
I have not been able to get anything to work correctly so far. Can anyone point me in the right direction with this? Below are a couple of things I have tried so far...
**
PS C:\Windows\system32> $name= gc env:computername
$computers= get-content -path c:\users\person\Desktop\book2.txt
$csvfile = "c:\temp\$name.csv"
foreach ($computer in $computers) {Get-ChildItem -recurse -filter "C:\Users\*.locky"}
export-csv -filepath $csvfile
**
*
Get-ChildItem : Second path fragment must not be a drive or UNC name.
Parameter name: path2
At line:4 char:36
+ foreach ($computer in $computers) {Get-ChildItem -recurse -filter "C:\Users\*.lo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (C:\Windows\system32:String) [Get-ChildItem], Argume
ntException
+ FullyQualifiedErrorId : DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand
Export-Csv : A parameter cannot be found that matches parameter name 'filepath'.
At line:5 char:12
+ export-csv -filepath $csvfile
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ExportCsvCommand
*
AND
C:\Windows\system32> ForEach ($system in (Get-Content C:\temp\Book2.txt))
if ($exists in (Test-Path \\$system\c$\Users\*.locky))
{
Get-Command $exists | fl Path,FileVersion | Out-File c:\temp\results.csv -Append
}
At line:1 char:53
+ ForEach ($system in (Get-Content C:\temp\Book2.txt))
+ ~
Missing statement body in foreach loop.
At line:3 char:14
+ if ($exists in (Test-Path \\$system\c$\Users\*.locky))
+ ~~
Unexpected token 'in' in expression or statement.
At line:3 char:14
+ if ($exists in (Test-Path \\$system\c$\Users\*.locky))
+ ~~
Missing closing ')' after expression in 'if' statement.
At line:3 char:55
+ if ($exists in (Test-Path \\$system\c$\Users\*.locky))
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingForeachStatement
Wrong -filter parameter in Get-ChildItem cmdlet. Next code snippet should work:
foreach ($computer in $computers) {"\\$computer\C$\Users"|`
Get-ChildItem -recurse -filter "*.locky"}

PowerShell error: Cannot move item because the item at xxx is in use

I am using PowerShell, I need to Move-Item a folder after some operation is executed (in my case delete all empty folders).
When I run the following script I get an error:
Move-Item : Cannot move item because the item at 'C:\Projects\xxx\aaa' is in use.
At line:58 char:1
+ Move-Item $dist $fin
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand
Any idea how to solve this?
# delete all empty folder recursively at any levels
do {
$dirs = gci $tdc -directory -recurse | Where { (gci $_.fullName).count -eq 0 } | select -expandproperty FullName
$dirs | Foreach-Object { Remove-Item $_ }
} while ($dirs.count -gt 0)
# move folder
Move-Item $dist $fin
Write-Host '- Moving files executed.'
You need to move out of the directory that you are working in.
cd ~
Then use the Move-Item command