Using PowerShell Where-Object not working with Get-ChildItem - powershell

I am trying to get directories that contain "COO", but after adding the Where-Object nothing is returned, the script is looping through each of the folders but nothing makes it to the file when the Where-Object is in place.
This works:
Get-ChildItem -Path $path -Recurse -Directory | select Fullname | Export-csv c:users\shay\desktop\test.csv
This doesn't:
Get-ChildItem -Path $path -Recurse -Directory | Select Fullname | Where-Object {$_.Fullname -like 'COO'} | Export-Csv c:users\shay\desktop\test.csv

In order to use the -Like parameter you'll need to include a wildcard (*), try the following:
Get-ChildItem -Path $path -Recurse -Directory | select Fullname | where-object {$_.Fullname -like '*COO*'} | Export-csv c:users\shay\desktop\test.csv
You could also potentially move the expression to the Get-ChildItem portion of this snippet.
Get-ChildItem -Path $path -Recurse -Directory -Filter '*COO*' | select Fullname | Export-csv c:users\shay\desktop\test.csv

Related

Powershell delete all folders except one

I tried
Get-ChildItem -Path 'C:\temp' -Recurse Select Name | Where {$_ -notlike 'C:\temp\one*'} | sort length -Descending | Remove-Item -force
but it doesn't work
Get-ChildItem : A positional parameter cannot be found that accepts argument 'Name'
What's wrong
You were missing a |
Get-ChildItem -Path 'C:\temp' -Recurse | Select -ExpandProperty FullName | Where {$_ -notlike 'C:\temp\one*'} | Remove-Item -force
Try this with -Exclude (And why sort when deleting files?)
Get-ChildItem -Path 'C:\temp' -Recurse -Exclude 'C:\temp\one*' | Remove-Item -force
Use the function below:
Function Delete-Except
{
$path = ""
$exceptions = #(
#Enter files/folders to omit#
)
try:
Get-ChildItem $source -Exclude $exceptions| Remove-Item $_ -Force -Recurse
catch:
Write-Host "Delete operation failed." - Foregroundcolor Red
Pause
}

PowerShell cmdlet to get a name of recently zipped folder?

I have tried
$latest1 = gci $path -Include *.zip| ? { $_.PSIsContainer } | sort CreationTime -desc | select -f 1
but $latest1 is giving blank output.
The -Include switch will only work if the path ends in \* or when used together with the -Recurse switch.
Since in your case, you are only looking for zip files, I would use the -Filter parameter.
$latest1 = Get-ChildItem $path -Filter '*.zip' -File |
Sort-Object CreationTime -Descending | Select-Object -First 1
For PowerShell version below 3.0 use
$latest1 = Get-ChildItem $path -Filter '*.zip' | Where-Object { !$_.PSIsContainer } |
Sort-Object CreationTime -Descending | Select-Object -First 1
If a zip file was found in the path, $latest should now be a FileInfo object with properties like FullName, BaseName etc.
Try changing your where-object filter or removing it
$latest1 = gci $path -Include *.zip| ? { $_.PSIsContainer -eq $false } | sort CreationTime -desc | select -f 1
$latest1 = gci $path -Include *.zip| | sort CreationTime -desc | select -f 1

Adding File Properties such as the Creator or Author

As of now I have a script that will pull and export all files that are older than 7 years:
get-childitem -Path C:\ -recurse|
where-object {$_.LastWriteTime -lt (get-date).Addyears(-7)} |
Select-Object FullName, LastWriteTime, #{N='Owner';E={$_.GetAccessControl().Owner}}, #{N='Author';E={$_.GetAccessControl().Author}}|
Export-Csv C:\file.csv
Now, I need help adding the Creator or Author from the file properties. The #{N='Author';E={$_.GetAccessControl().Author}} doesn't work.
Adjusted so that you're retrieving ACLs properly. I don't think it's possible to grab the original author, however.
Get-ChildItem -Path C:\ -Recurse |
Where-Object {$_.LastWriteTime -lt (Get-Date).AddYears(-7)} |
Select-Object #( 'FullName'
'LastWriteTime'
#{ N = 'Owner'
E = { (Get-Acl -Path $_.FullName).Owner }
}
) |
Export-Csv -Path C:\file.csv

Out-File only if Results are available. No Zero K files

Is there a way when you use Get-ChildItem with a Where-Object clause to have it produce the results in a text file only if there are results?
Get-ChildItem -path \\$server\e$ -Recurse | Where-Object {$_.name -eq help.txt} | `
out-file "c:\temp\$server.txt"
The above will produce a file regardless if there are results. I'm having trouble telling implementing the logic to only create when results are available.
You can't do it that way. You'll have to do it in 2 parts:
$results = Get-ChildItem -path \\$server\e$ -Recurse | Where-Object {$_.name -eq help.txt}
if ($results) {
$results | out-file "c:\temp\$server.txt"
}
Seems to work how you want if you use Set-Content instead of Out-File.
Get-ChildItem -path \\$server\e$ -Recurse | Where-Object {$_.name -eq help.txt} |
Set-Content "c:\temp\$server.txt"
#or
gci -R \\$server\e$ |? Name -eq "help.txt" | sc "c:\temp\$server.txt"

PowerShell Filter on multiple parameters

I was wondering if is possible to specify multiple search filters as once. For example, I have this line of code that finds all files that have the "&" symbol.
get-childitem ./ -Recurse -Filter "*&*" |
? { $_.PSIsContainer } |
Select-Object -Property FullName
I'd like to extend this so that I can search one time and find files with other symbols like %,$,#, etc. I want to find files that have any of these symbols, not neccesarly files that have all of them so I assume there needs to be an OR somewhere. I tried the below code but it didn't seem to work for me:
get-childitem ./ -Recurse -Filter "*&*" -Filter "%" |
? { $_.PSIsContainer } |
Select-Object -Property FullName
You can use the -match operator and a regex for this:
Get-ChildItem -Recurse |
Where { !$_.PSIsContainer -and ($_.name -match '&|%|\$|#')} |
Select-Object -Property FullName
If you are on PowerShell v3 or higher you can simplify this a bit:
Get-ChildItem -Recurse -File |
Where Name -match '&|%|\$|#' |
Select-Object -Property FullName
If you have V3 or better, you can leverage the "globbing" wildcard feature:
get-childitem './*[&%$#]*' -Recurse | where {$_.PSIsContainer}
If you've got V4, you can dispense with the $_.PSIsContainer filter and use the -Directory switch:
get-childitem './*[&%$#]*' -Recurse -Directory