Write to the output between two pipeline - powershell

I'm trying to write to the output (echo) within two pipe to have a trace of what I'm doing but I have an error.
Get-ChildItem $path -Recurse |
Where-Object { $_.Name -match '.+?\.log\.(\d{4})-(\d{2})-(\d{2})$' -and $_.LastWriteTime -lt $deleteDate } |
Write-Output "deleting file" -PassThru |
Remove-Item
but I have this error :
Write-Output : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
I also tested in the Foreach-Object function
Get-ChildItem $path -Recurse |
Where-Object { $_.Name -match '.+?\.log\.(\d{4})-(\d{2})-(\d{2})$' -and $_.LastWriteTime -lt $deleteDate } |
Foreach-Object {
echo "deleting $($_.Name)";
Remove-Item($_);
}
but here nothing is printed in the console (but the script finish without error and deleted the files I wanted)
So how can I print to thje output wihtin two Pipe ?
Thx

Write-Output sends output to the pipeline. It accepts input via pipeline or argument, but not both, so you can do this:
Write-Output "Test"
or this:
"Test" | Write-Output
but not this:
"Test" | Write-Output "Test"
Solution:
Get-ChildItem $path -Recurse | where {
$_.Name -match '.+?\.log\.(\d{4})-(\d{2})-(\d{2})$' -and $_.LastWriteTime -lt $deleteDate
} | foreach {
# Write-Host writes to the console only
Write-Host "Deleting $($_.Name)"
Remove-Item $_
}
Mind that Remove-Item expects a string argument, so everything you pass will be converted to string. Casting a FileInfo object to string might not always return the full path (because of its implementation of ToString()) depending on how it was created!
In the example above, it is safe to use, but this for instance will throw an an exception (if your current working directory is not $path):
(Get-Item $path).GetFiles() | foreach { Remove-Item $_ }
So it's always safest to use $_.FullName.

Related

How to check duplicate multiple file using powershell?

I want to check duplicate file.If the condition of the file like this, it means duplicate. The same name but different extension.
AAA18WWQ6BT602.PRO
AAA18WWQ6BT602.XML
I can figure out this case with my script. But I have problem if I have this more than 1 .XML file like this
AAA18WWQ6BT602.PRO
AAA18WWQ6BT602.XML
AAA18WWQ6BT601.XML
AAA18WWQ6BT604.XML
This case, it will not detect that file AAA18WWQ6BT602.PRO and AAA18WWQ6BT602.XML duplicated.
Anyone can help me please.
Thanks
$duplicate = #()
#(Get-ChildItem "$Flag_Path\*.xml") | ForEach-Object { $duplicate += $_.basename }
if(Test-Path -Path "$Flag_Path\*$duplicate*" -Exclude *.xml)
{
Get-ChildItem -Path "$Flag_Path\*$duplicate*" -Include *.xml | Out-File $Flag_Path\Flag_Duplicate
Write-Host "Flag duplicated, continue for Error_Monitoring"
pause
Error_Monitoring
}
else{
Write-Host "Flag does not duplicate, continue the process"
}
The -Include parameter only works if the path on Get-ChildItem ends in \* OR if the -Recurse switch is used.
The following should do what you want:
$flagFolder = 'D:\*'
$dupeReport = 'D:\Flag_Duplicate.txt'
$duplicates = Get-ChildItem -Path $flagFolder -File -Include '*.xml', '*.pro' |
Group-Object -Property BaseName | Where-Object { $_.Count -gt 1 }
if ($duplicates) {
# output the duplicate XML to Flag_Duplicate.txt
$duplicates.Group | Where-Object {$_.Extension -eq '.xml' } | ForEach-Object {
$_.FullName | Out-File -FilePath $dupeReport -Append
}
# do the rest of your code
Write-Host "Flag duplicated, continue for Error_Monitoring"
Error_Monitoring
}
else {
Write-Host "Flag does not duplicate, continue the process"
}
Your script does not iterate correctly. You need to have an iteration to check. The Test-Path logic looks mixed up to me. I tried to keep as much of your code as possible.
This script checks for a any xml basename filename against any suffix duplicate (not only pro):
$Flag_Path = "C:\dir_to_be_checked"
$xmlFilesArray = #()
$allFilesExceptXml = #() # all files excluding xml files
# Get all the xml files
Get-ChildItem -Path $Flag_Path -Include "*.xml" | ForEach-Object { $xmlFilesArray += $_.basename }
# Get all files from the directory the xml files
Get-ChildItem -Path $Flag_Path -Exclude "*.xml" | ForEach-Object { $allFilesExceptXml += $_.basename }
# Iterate over list of files names without suffix
ForEach ($xmlFile in $xmlFilesArray) {
ForEach ($fileToCheck in $allFilesExceptXml) {
If ($xmlFile -eq $fileToCheck) {
# logging the duplicate file (specifying utf8 or the output would be UTF-16)
Write-Output "$Flag_Path\$xmlFile.xml" | Out-File -Append -Encoding utf8 $Flag_Path\Flag_Duplicate
Write-Host "Flag duplicated, continue with duplicate search"
# pause
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Error_Monitoring
} Else {
Write-Host "Flag is not duplicated. Continue with the search."
}
}
}

Write folder name and subfolder name being deleted to output file

I have written the below with the intention of deleting all folders in a directory that have a creation date of 2 days or more and log this in an output file if it is successful or not.
The script works as I would like with the exception that the name of the file will not show in the output file. All that is displayed is 'Deletion of Failed/Successful'
$dump_path = "C:\desktop"
$max_days = "-2"
$curr_date = Get-Date
$del_date = $curr_date.AddDays($max_days)
ForEach-Object {
$filename = $_
Get-ChildItem $statfolder\$_ -Recurse | Where-Object {
$_.CreationTime -lt $del_date
} | Remove-Item -Recurse -Force
if ($? -eq $false) {
echo "$Deletion of $filename Failed" |
Out-File -Append C:\Logs\DELETION_FAIL_K_$(Get-Date -Format `"dd-MMM-yyyy`").txt
} else {
Write-Output "Deletion of $filename Successful" |
Out-File -Append C:\Logs\DELETION_SUCCESS_K_$(Get-Date -format `"dd-MMM-yyyy`").txt
}
}
I would ideally like the log to display the parent folder name and a list of the sub folders in next level down only. Is this possible?
Eg. the log would read
Deletion of folder 12-Jan-2017 containing sub folders R2015, R2086 was Successful
If the sub folders in the next level cannot be added then just the below would be great:
Deletion of folder 12-Jan-2017 was Successful
The way you're using ForEach-Object the current object variable $_ (and consequentially the variable $filename) is never populated. Where would you expect the value to come from?
Feed the output of Get-ChildItem | Where-Object into ForEach-Object, but sort the results by full name first, so that nested folders are deleted before their parents.
Get-ChildItem $statfolder\$_ -Recurse | Where-Object {
$_.PSIsContainer -and
$_.CreationTime -lt $del_date
} | Sort-Object FullName | ForEach-Object {
$folder = $_.FullName
"Deleting folder '$folder'."
Remove-Item $folder -Recurse -Force -WhatIf
}
With the -WhatIf switch present you'll be doing a dry-run, just echoing what would be deleted without actually deleting it. After you verified that everything would work as intended remove the switch and re-run.

Get-ChildItem error handling when using long file paths

I am trying to handle errors when scanning through folders. Let's say I have something like:
Get-ChildItem $somepath -Directory | ForEach-Object {
if(error occurred due to too long path) {
skip this folder then
} else {
Write-Host $_.BaseName
}
}
When I do this I print the folders in $somepath until one of them is too long and then the loop stops. Even when using SilentlyContinue. I want to print even after reaching a folder that is too long.
If you can install a non-ancient PowerShell version (3.0 or newer), simply prepend the path with \\?\ to overcome the 260-character limit for full path:
Get-ChildItem "\\?\$somepath" | ForEach {
# ............
}
You could try ignoring the files longer 260 characters by using the Where-Object cmdlet.
Get-ChildItem $somepath -Directory -ErrorAction SilentlyContinue `
| Where-Object {$_.length -lt 261} `
| ForEach-Object { Write-Host $_.BaseName }
Or you could use the following (Ref).
cmd /c dir $somepath /s /b | Where-Object {$_.length -lt 261}
I will add my solution since the neither on this page worked for me. I am using relative paths, so I can't use the \\ prefix.
$TestFiles = Get-ChildItem $pwd "*Tests.dll" -Recurse | Where-Object {$_.FullName.length -lt 261} | Select-Object FullName -ExpandProperty FullName | Where-Object FullName -like "*bin\Release*"
Write-Host "Found TestFiles:"
foreach ($TestFile in $TestFiles) {
Write-Host " $TestFile"
}

Remove all folders .old

I'm trying to delete all folders in \\kiewitplaza\vdi\Appsense_profiles that end with .old. The piece I have that says Write-Host $name is just me trying to test before I delete anything.
$name = Get-ChildItem "\\kiewitplaza\vdi\Appsense_profiles"
if ($name.name.EndsWith(".old")) {
Write-Host $name
#Remove-Item "\\kiewitplaza\vdi\Appsense_profiles\$name.old"
}
Get-ChildItem produces a list of objects. Use a pipeline for processing that list:
Get-ChildItem '\\kiewitplaza\vdi\Appsense_profiles' |
Where-Object { $_.Name -like '*.old' } |
Remove-Item

PowerShell Get-ChildItem and Skip

I'm writing a PowerShell script that deletes all but the X most recent folders, excluding a folder named Data. My statement to gather the folders to delete looks like this:
$folders1 = Get-ChildItem $parentFolderName |
? { $_.PSIsContainer -and $_.Name -ne "Data" } |
sort CreationTime -desc |
select -Skip $numberOfFoldersToKeep
foreach ($objItem in $folders1) {
Write-Host $webServerLocation\$objItem
Remove-Item -Recurse -Force $parentFolderName\$objItem -WhatIf
}
This works great when I pass a $numberOfFoldersToKeep that is fewer than the number of folders in the starting directory $parentFolderName. For example, with 5 subdirectories in my target folder, this works as expected:
myScript.ps1 C:\StartingFolder 3
But if I were to pass a high number of folders to skip, my statement seems to return the value of $parentFolderName itself! So this won't work:
myScript.ps1 C:\StartingFolder 15
Because the skip variable exceeds the number of items in the Get-ChildItem collection, the script tries to delete C:\StartingFolder\ which was not what I expected at all.
What am I doing wrong?
try this:
$folders1 = Get-ChildItem $parentFolderName |
? { $_.PSIsContainer -and $_.Name -ne "Data" } |
sort CreationTime -desc |
select -Skip $numberOfFoldersToKeep
if ($folder1 -neq $null)
{
foreach ($objItem in $folders1) {
Write-Host $($objItem.fullname)
Remove-Item -Recurse -Force $objItem.fullname -WhatIf
}
}
I gave #C.B. credit for the answer, but there's another way to solve the problem, by forcing the output of Get-ChildItem to an array using the #( ... ) syntax.
$folders1 = #(Get-ChildItem $parentFolderName |
? { $_.PSIsContainer -and $_.Name -ne "Data" } |
sort CreationTime -desc |
select -Skip $numberOfFoldersToKeep)
foreach ($objItem in $folders1) {
Write-Host $webServerLocation\$objItem
Remove-Item -Recurse -Force $parentFolderName\$objItem -WhatIf
}
This returns an array of length zero, so the body of the foreach statement is not executed.
As C.B. noted in the comments above, the problem is that if you pass a null collection into a foreach statement in PowerShell, the body of the foreach statement is executed once.
This was completely unintuitive to me, coming from a .NET background. Apparently, it's unintuitive to lots of other folks as well, since there's bug reports filed for this behavior on MSDN: https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=281908&SiteID=99
Apparently, this bug has been fixed in PowerShell V3.