Powershell: How to rename all ".jpg" within folders? - rename

I have 1 picture in each of the folders and i want to rename them all to "ISO".
i want it to look something like
Folder1
-sub1
ISO.jpg
-sub2
ISO.jpg
-sub3
ISO.jpg
and so on.
right now all the pictures under sub# have different names, i want to rename them all to ISO
This is what i know to do with renaming
Dir | Rename-Item –NewName { $_.name –replace “ “,”ISO” }
i know this will replace all the spaces with ISO, but that's not what i want.
my main question is what the first argument for -replace should be regardless of the file name.

Here my version , obviously if there are more jpegs inside a subfolder it could raise an exception :
Get-ChildItem .\sub*\*.jpg | rename-item -NewName ISO.jpg

Related

List files with strings and then ALL files checked with powershell

I want to search for all files, of .exe.config for certain strings. I want to list the files with those strings and then list all of the files that were checked. I can get it to find the files with the strings with:
Get-ChildItem -Path c:\ -I *.exe.config -R| Select-String 'string1','string2'
My issue, I can't figure out how to get it to show all the .exe.config files that it checked (without searching the computer again). I thought I could save the intial search results into a variable. Then run through that with a for loop, but I can't even get the right info into a variable. I tried several variations of below, but $files is always empty. Also, I'm not sold on this approach, so if anyone has a completely different method, that would be fine
$files = (Get-ChildItem -Path c:\ -I *.exe.config -R).Path
$files = (Get-ChildItem -Path c:\ -I *.exe.config -R | select fullname)
Use the common -OutVariable parameter to capture Get-ChildItem's output in a variable of your choice, separately from what the pipeline overall outputs:
Get-ChildItem c:\ -Include *.exe.config -Recurse -OutVariable files |
Select-String 'string1','string2'
Variable $file now contains all System.IO.FileInfo instances output by Get-ChildItem; note how you must pass the variable name without the $ sigil to -OutVariable.
A small caveat is that -OutVariable, as of PowerShell [Core] 7.0:
collects multi-object output in a System.Collections.ArrayList instance rather than in a regular PowerShell array of type [object[]] (fixed-size array of System.Object instances).
even collects single-object output that way (wrapped in a collection) - whereas direct pipeline output just emits the object itself.
See this answer for background information and a workaround.

Removing duplicate shortcuts containing a specific string

We have a lot of users with duplicate files on their Desktop (Windows 10). I want to create a script which runs every X minutes or just once after logging on to the system.
Our users are currently seeing the following shortcuts (.lnk):
Dummy.lnk (correct shortcut, should remain unchanged)
Dummy - copy.lnk (duplicated shortcut, should be removed)
Dummy-OFFICE-QB4V70A.lnk (duplicated shortcut containing the name of the computer, should be removed)
So in short I want to keep the 'original' shortcuts but I want to remove all shortcuts containing '- copy.lnk' and/or '-computername.lnk'.
I also tried using the %computername% variable but I have no idea how to implement this in my code. Every computer has a different and unique name so I can not use pre-defined computernames.
Any tips on how I can achieve this? I have tried using the code below, but this removes all .lnk (shortcut) files and not just the duplicates.
#(Get-ChildItem Dummy.lnk | Select-String -Pattern "OFFICE" | Select-Object -ExpandProperty path -Unique) | ForEach-Object{Remove-Item -Force -LiteralPath $_}
I prefer using Powershell so that I can deploy the script using Group Policy.
I would use the Where-Object cmdlet to filter all shortcuts you want to remove and pipe them to the Remove-Item cmdlet:
Get-ChildItem -Path 'c:\yourPath' -Filter '*.lnk' |
Where-Object { $_.Name -match '- Copy\.lnk$' -or $_.Name -like "*$($env:Computername)*"} |
Remove-Item

How to search a string in multiple files and return file name with line number/text in an Excel or csv in Powershell

I have found an answer that gives the list of files in powershell here
How to search a string in multiple files and return the names of files in Powershell?
but I need to know the line number and line text also where that text is found. After this I need to write the output to an excel or csv.
edit:
Get-ChildItem -recurse | Select-String -pattern "ftp" | Write-Host script is giving me the output as C:\codes\prog1.txt:10:FTP but I want the output as path - C:\codes\prog1.txt line number-10 Line text-FTP "server1" in Excel
Try this (don't know if you only want the filename or the path to the file, just remove the one you dont want):
Get-ChildItem -recurse | Select-String -pattern "string" | Select-Object path,line,linenumber,filename | Export-Csv -Path c:\somepath\result.csv

Powershell - Files not older than

I´m trying to make a Powershell script that reports if there´s a file not older than x hours which contains some string pattern. I made this:
Get-ChildItem C:\Folder -recurse | Select-String -pattern "err" | group path | select name | Where {$_.LastWriteTime -gt (Get-Date).AddHours(-12)}
Problem is that the last part of code which should select only files younger than x hours does not work - shows no files. When I change -gt to -lt it shows every file in the folder which contains pattern including younger than defined hours.
Does anyone has a solution please?
Thank you in advance
Your pipeline is in the wrong order. You are piping a collection of strings to Group-Object, which pipes a different collection to Select-Object, etc. Your call to Where-Object is receiving the output of Select-Object, which is a collection of PSCustomObjects.
What you want is to pipe the file objects themselves to Where-Object, and then pass those file objects down the pipeline:
Get-ChildItem C:\Folder -recurse |
Where {$_.LastWriteTime -gt (Get-Date).AddHours(-12)} |
Select-String -pattern "err" | group path | select name

Powershell Select-String and then obtain data on returned files

I admit I am quite tired today but even that is no excuse for the nightmare i seem to be creating for myself with Powershell at present.
Basically the aim is to search a directory for a string that is contained in some word documents. Then I need to return the file name, created date and the last write time. Easy I thought but at some stage I have gone well off track and am still climbing.
What I have done so far is run the search and export the path variable to a text file giving me the path to the files that contain the string. I have then tried to use a foreach loop to load the contents of the file and run a Get-ChildItem against each entry piped to a Select-Object Name,CreationTime,LastWriteTime. I have finally > this to a text file. However it now seems to be returing the info for every file in teh directory and not just those that contain the string I am searching for. I get the feeling I am vastly over complicating this as i tend to with these things. Any help greatly appreciated.
Get-ChildItem -Recurse -Include *.doc | Select-String "Shiba" | select-object path > C:\TRCALM\shibapath.txt
$files = get-content C:\TRCALM\shibapath.txt
foreach($i in $files){gci $i | select-object Name,CreationTime,LastWriteTime > C:\TRCALM\SHIBADates.txt}
You were almost there:
$files = (Get-ChildItem -Recurse -Include *.doc | Select-String "Shiba"| select-object path )
$k = foreach($i in $files){ (gci $i.Path | select-object Name,CreationTime,LastWriteTime) }
$k > C:\pst\SHIBADates.txt

Resources