Return to site

Powershell check if file contains string

broken image
broken image

Get-ChildItem -Path $directoryToTarget -Filter *. I assumed this meant that $_ was an array so I tried to create a loop to go through it and check each file at a time however it told me it was Unable to index into an object of type System.IO.FileInfo.Ĭould anyone please tell me how I could alter the code below to check if ever file contains the wordToChange and then apply the change it it does.

broken image
broken image

To counter this I tried to put in a check to see whether the word is present or not in the file before I get/set the content (shown below) however it tells me that ] doesn't contain a method named 'Contains'. If there is more than one occurrence, it would return an array of the lines in which the text was found. What I have written below does this however my version control notices a change in every single file regardless of whether it contains an instance of the word to change. In a case where the text ERROR only occurs once in the file, the Where-Object cmdlet would return a string value containing the line in which the text was found. properties files to an instance of another given word. I am trying to create a powershell script which looks through all files and folders under a given directory it then changes all instances of a given word in.