Enterprise find files (via powershell) exclude Tempory Internet Files - powershell

I am trying to run this command via powershell on every computer on my network.
I am running into a problem with Temporary Internet Files, providing too many false positives.
Can anyone suggest a way to improve this command?
dir C:\Users\ /S /B | findstr /i "".t.st."" > "C:\test.txt"
One suggestion, was
dir C:\Users\ /S /B | findstr /i "".t.st."" > "C:\test.txt"
dir C:\Users\ /S /B | findstr /i "".t.st."" | findstr ""Temporary Internet Files"" > "C:\test2.txt"
fc C:\test.txt C:\test2.txt > C:\results.txt
But running through tests, it didn't give me the results I was looking for. I still had duplications. Or it would say the files are too different.
Thanks!

If you are running this through PowerShell then why not use PowerShell commands? Your examples are using the legacy DIR command. You could use Get-ChildItem instead. You can use -Exclude to skip a folder. You could use -filter or -include to find the files you want. Worse case is to get the files and then pipe to where object perhaps using a regex pattern to filter out the files you want. I can't tell from your commands what files you are looking for.

Related

Searching for files with the same name and 3 letter extensions

So for my task i have to find all files on my C: partition that start with the word 'printer'and they have to have an extension that contains exactly 3 letters i also have to do the same for the .dll extension only
That is a lot of work for PowerShell (my old opinion). :)
If you do it in the native operating system it is:
dir printer*.??? /s /A-D
or
dir printer*.* /s /A-D
(In both cases from the root as /s denotes subfolders. You could add some other stuff for hidden files or folders but /? will give that to you. /A-D removes directories.)
You can pull that in to PowerShell with: cmd /r dir printer*.??? /s /A-D
Now if you do want to use PowerShell Natively you can read up but it'll be like this:
Get-ChildItem -Path C:\ -Include printer*.??? -File -Recurse -ErrorAction SilentlyContinue
You can swap out *.??? for *.dll in the code to get the results you want.

Check if a file with a pattern is available in the directory using batch script

A file is dropped in a directory when another job finished processing it.
The requirement is to run a batch script to check whether the file is available for today. and if available, I need to execute certain batch script. (Example: File with Naming pattern ABC-D*.txt should be available with modification date=today)
What I have figured out till now is:
FOR /F "tokens=1,2,3* Delims=/ " %%I in ('DATE /T') DO set TODAY=%%I-%%J-%%K
xcopy C:\BatchJobs\Odd*.* /L /D:%TODAY%
Running this is giving the output:
C:\BatchJobs\OddEven.txt
File cannot be copied onto itself
0 File(s)
C:\BatchJobs\OddEven.txt, showing in console is what I need. But I need to store it in some file or in some variable to be able to use this path later in my batch script. Can somebody help me in storing this file path in a variable or a file? Or suggest some other ways to achieve this goal?
You could use forfiles which is capable of filtering files by their last modification date, but not regarding the time:
forfiles /P "D:\ROOT" /M "ABC-D*.txt" /D +0 /C "cmd /C echo #file"
Instead of echo you can state your batch script to execute.
This code will identify "ABC-D*.txt" files last written today. Place the code below into a file such as doit.ps1 (but choose a better name). I did an attrib command on the file, but you will want to do something else.
$srcdir = 'C:\src\t\empty'
Get-ChildItem -File -Path $srcdir -Filter 'ABC-D*.txt' |
Where-Object { $_.LastWriteTime -ge [datetime]::Today } |
ForEach-Object {
# Do something to the $_ file
& attrib $_
}
Then, you can run it from a cmd shell with:
powershell -NoProfile -File doit.ps1

Windows cmd: Piping dir output into start

So suppose I know there's some file(s) (all with the same extension) in my directory which I want to run only I don't know their name.
I could execute the following to get all the relevant file names (.ext is just an arbitrary extension here)
dir /b /a-d *.ext
And then I could call
start
on all those files.
But I'd like to skip a step and simply pipe the output from dir into start.
I've tried
dir /b /a-d *.ext | start
but all that does is open an empty shell. Any suggestions?
For completeness, PowerShell:
Get-ChildItem *.exe | ForEach-Object { Start-Process $_ }
shorter (with aliases):
gci *.exe | % { start $_ }
you can
dir /b /a-d *.ext > start.bat
This will generate a start.bat file. Then:
call start.bat
By running the file you will run all apps.

using "CON" as filename

I was copying a huge number of png and txt files using Copy-Item cmdlet, and sadly I discovered that a funny programmer decided to use "CON" as file name to recap connection information.
Given that "con" is a reserved word and Copy-Item returns:
Copy-Item : Cannot process path 'xxx\con.txt' because the target represents a reserved device name.
and given that this name can't be changed and it's used in every folder I need to copy,
Is there a way to copy all these "con.cfg" and "con.txt" files using Powershell?
I googled but I found only advice like "Don't use con!" or "Don't use Powershell to copy these files".
I haven't been able to find a solution for PowerShell yet, but you should be able to rename the files via command prompt using something like this:
ren \\.\<absolute path> <new name>
So for example:
ren \\.\C:\stuff\con.cfg stuff.cfg
You could invoke the command prompt through PowerShell, of course:
cmd /c "ren \\.\C:\stuff\con.cfg stuff.cfg"
And obviously you could use PowerShell variables in there if you wanted
$dir = "C:\stuff"
cmd /c "ren \\.\$dir\con.cfg stuff.cfg"
You could try referring to them using a wildcard: *on ?
Example:
ls | ? {$_.Name -match "*on.cfg"} | del
Regex example:
ls | ? {$_.Name -match "^\won\.cfg"} | del

Powershell 2.0 out-file formatting nightmare

How can I get Powershell to output a file IDENTICAL to the file produced by the following command?
dir /s /b /a-d *.* > C:\files.txt
should be easy, right?!!
EDIT:
I found ps was truncating the output based on the screen buffer width. Fix that with format-table and it pads with spaces... try format-list and you get property headings...you get the idea.
Does it have to be exact? Why? As the saying goes, if you're parsing strings in Powershell, you're probably doing something wrong...anyway...
1) Just call into cmd.exe.
PS> cmd /c "dir /s /b /a-d *.* > c:\files.txt"
2) I believe you can get the same results from native Powershell. But I can't be responsible for testing every edge case with NTFS junctions, hidden files, etc.
PS> gci -r | ?{ !$_.psiscontainer } | %{ $_.fullname } | out-file c:\files.txt
I personally hate the fact you can't use "select" to retrieve the FullName property without weird side effects on downstream cmdlets. If the pointlessness of the foreach loop bothers you as much as it does me, use Get-PropertyValue from PSCX or Linq-Select from Josh Einstein.
There are many ways to write information to a file. One is Set-Content, which does not have the width problem. Also, converting a FileInfo object to a string results in the FullName.
dir * -r | ?{!$_.PSIsCcontainer} | Set-Content C:\files.txt
The reason you have a problem with Out-File is that all the Out-* cmdlets use the automatic formatting views. Those views are created with the console in mind.