I search for a file, e.g. the hosts file:
cd c:\Windows\System32
gci -Recurse | ? {$_.Name -eq 'hosts'}
Now I want to open the file in notepad, so I tried:
gci -Recurse | ? {$_.Name -eq 'hosts'} | notepad.exe $_.FullName
This errors. Is there a way to do this, as a one-liner?
notepad.exe does not accept pipeline output input
Get-ChildItem -Recurse -ErrorAction SilentlyContinue |
Where-Object -FilterScript { $_.Name -eq 'hosts' } |
Foreach-Object -Process { notepad.exe $_.FullName }
I would recommend using -Filter on get-childitem for this. It would greatly improve performance on the scriptlet.
-#matt
Get-ChildItem -Filter Hosts -Recurse -ErrorAction SilentlyContinue |
ForEach-Object -Process { notepad.exe $_.FullName }
Related
I am trying to recursively search for files on a windows 7 machine + network drives attached to it while excluding certain folders i.e. C:\windows & all recursive folders in this such as system32.
I know this question has been asked before but following the answers has not helped and I am still left with a blank variable.
Here are the combinations I have tried:
$AllDrives = Get-PSDrive
$files=#("*.xml",*.txt)
foreach ($Drive in $AllDrives) {
if ($Drive.Provider.Name -eq "FileSystem") {
$filenames = Get-ChildItem -recurse $drive.root -include ($files) -File | Where-Object {$_.PSParentPath -notlike "*Windows*" -and $_.PSParentPath -notlike "*Microsoft*"
}
}
I have also tried these combinations:
$filenames = Get-ChildItem -recurse $drive.root -include ($files) -File | Where-Object {$_.PSParentPath -notmatch "Program Files|Users|Windows"}
$exclude_pattern = $drive.root + "Windows"
$filenames = Get-ChildItem -Force -Recurse -path $drive.root -Include $files -Attributes !Directory+!System -ErrorAction "SilentlyContinue" | Where-Object { $_.PSIsContainer -eq $false } | Where-Object { $_.FullName -notmatch $exclude_pattern }
Unfortunately, after an amount of time has elapsed, when I type $filename into the terminal nothing has been assigned to it.
I want to know how to log the actions from this script to a text file because I don't know how to do it as the cmdlet Start-Transcript doesn't work for me and I wasn't able to find a solution on the Internet.
The problem is that the Where-Object cmdlet doesn't output anything captured by Get-ChildItem.
Does anybody has a good idea to solve this?
$limit = (Get-Date).AddDays(-30)
$path = Split-Path -Parent $MyInvocation.MyCommand.Definition
Get-ChildItem -Path $path -Recurse -Force | Where-Object {
!$_.PSIsContainer -and
$_.LastWriteTime -lt $limit
} | Remove-Item -Force
Get-ChildItem -Path $path -Recurse -Force | Where-Object {
$_.PSIsContainer -and
(Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object {
!$_.PSIsContainer
}) -eq $null
} | Remove-Item -Force -Recurse
try something like this
$limit = (Get-Date).AddDays(-30)
$path =Split-Path -Parent $MyInvocation.MyCommand.Definition
Get-ChildItem $path -file -recurse -force | where LastWriteTime -lt $limit |
Tee-Object -FilePath "c:\temp\deleted.txt" -Append | Remove-Item
Get-ChildItem $path -directory |
where {(Get-ChildItem $_.FullName -file -Recurse | select -First 1) -eq $null} |
Tee-Object -FilePath "c:\temp\deleted.txt" -Append | Remove-Item
howdy error666,
you can use use a few different methods ...
Tee-Object = fork the stream to a file
-PipelineVariable = accumulate the info in a variable
use a loop = put a log-to-file step in it
put a ForEach-Object in the pipeline
that can both log your info and do the Remove-Item.
the loop is the easiest to understand. [grin] however, if you want to keep it in a pipeline, you could add a ForEach-Object where the Where-Object scriptblock is and put both the filter test and the various actions in that block.
take care,
lee
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"
Have a script which search a given network location for specific file based on a given word. I need to adapt this and get the script to copy the file to another given network location.
Can someone help?
"`n"
write-Host "Search Running" -ForegroundColor Red
$filePath = "\\fileserver\mylocation$\folder"
"`n"
Get-ChildItem -Recurse -Force $filePath -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and ( $_.Name -like "*keyword*"
) } | Select-Object Name,Directory,CreationTime,LastAccessTime,LastWriteTime | Export-Csv "C:\scripts\searches\csv\27022014.csv" -notype
Spend some time looking at the documentation on Copy-Item. Run man copy-item -full at the prompt. Look at the examples at the bottom on the help.
Then modify your script to something like this:
Get-ChildItem -Recurse -Force $filePath -ErrorAction SilentlyContinue |
Where-Object {!$_.PSIsContainer -and ($_.Name -like "*keyword*") } |
Copy-Item -Dest \\fileserver2\difflocation
I am trying to get a list of all my files with a specific extension.
(...)$_.Extension -eq ".$ext"
I read extension from console to script.
My question is how to find any file with an extension of .*?
Edit:
Here's the rest of the code:
$setOfFolders = (Get-ChildItem -Path D:\ -Directory).name
Write-host "Set last write date " -ForegroundColor Yellow -NoNewline
$ostZmiana= read-host $exten = read-host "Set extensions "
ForEach ($f in $setOfFolders)
{
$saveTofile = "C:\Users\pziolkowski\Desktop\Outs\$f.txt"
Get-ChildItem -Path D:\Urzad\Wspolny\$f -Recurse | ? {$_.LastAccessTime -lt $ostZmiana -and $_.Extension -eq ".$exten"} | % {Get-acl $_.FullName} |sort Owner | ft -AutoSize -Wrap Owner, #{Label="ShortPath"; Expression= $_.Path.Substring(38)}} > $saveToFile
}
You can also use the -filter on Get-ChildItem:
Get-ChildItem -Filter "*.txt"
And you can specifiy recursion too:
Get-ChildItem -Recurse -Filter "*.txt"
The $_.Extension will see the file as, (in the case of a text file) .txt
If $ext is currently a valid extension, then .$ext would look like ..txt echoed out.
The easiest way to get a list of files with a specific extension in PowerShell is one of two, really.
C like syntax:
$myList
Get-ChildItem |`
Foreach-Object {
if ($_.Extension -eq ".txt") { $myList += $_}
}
PowerShell style:
Get-ChildItem | Where-Object {$_.Extension -eq ".txt"} | Do stuff
To get all files, as most files have an extension, just use Get-ChildItem or GCI or ls. (GCI and LS are aliases for Get-ChildItem).
To get all files with an extension, but not a specific extension:
Get-ChildItem | Where-Object {$_.Extension}
This evaluates as a bool, so true or false.
These are off the cuff, but should get you going in the right direction.