Search and Replace text in file using PowerShell

I want to schedule a script in PowerShell to search a directory for an XML file for a particular text string and replace it with a different text string and then save the file with the same name, the name of the file
will change each time the script is run on a daily basis. There will only be one XML file in the location when the scheduled script is run.
I have never used PowerShell but am familiar with batch file commands, can anyone please help!!
KevinS

Hi Kevin,
I’m writing to just check in to see if the suggestions were
helpful. If you need further help, please feel free to reply this post directly so we will be notified to follow it up.
If you have any feedback on our support, please
click here.
Best Regards,
Anna
TechNet Community Support

Similar Messages

  • In Pages, how to search and replace text involving invisible characters?

    In Pages documents, how to search and replace text involving invisible characters, colors and font sizes—a task which is so easy in Mircosoft Word?

    I read that an older version of Pages allowed users to enter special characters in the search/replace fields, but this did not work for me.
    Here: http://www.macworld.com/article/1156533/pagesspecialcharacters.html
    I still am looking for a way to do this.

  • Find and Replace text in files

    Is there a function that will find and replace a word in a few text files at once. E.G replace the word database1 with database2 without going into each script individually and doing and search and replace.

    Andrew, you are being so cruel, hardnuts indeed ;)
    I won't mention any names, but at least it's not as cruel as some people saying “You may read DBMS_STATS package specification, at least, to get details, if you're too lazy to read manuals.”
    Personally I prefer to use a development tool (TOAD, Pl/Sql Developer) instead of a Text type editor.

  • Search and Replace text

    Apologies for what is no doubt a simple question - I'm trying to help out IT and I'm not a programmer, although maybe one day...
    Anyway, I'm going to need to read a text file, and search for a string in that text file and replace it.  I see "replace" as a function but I'm not familiar with how to use it.  Poking around I found <cffile action=append> and used that to add a line to a text document.  (Just a notepad .txt)
    Ultimately I'll need to unzip a folder, search and replace the string in a specific file in there, and then re-zip the file, but we'll get to that later.  I managed to unzip a folder already, that's easy enough.  Putting it all together is my current challenge.
    I'm using ColdFusion Builder 2.0
    Thanks for any help you guys can give.

    file.txt: Contains several lines that all say "All Work And No Play Makes Jack A Dull Boy".
    <cffile action="read" file="C:\file.txt" variable="myFile">
    <cfset myFile = Replace(myFile,"Dull","Bored","all")>
    <cffile action="write" file="C:\file.txt" output="#myFile#" addnewline="no" fixnewline="no">
    <cffile action="read" file="C:\file.txt" variable="myNewFile">
    <cfoutput>#myNewFile#</cfoutput>
    ^_^

  • Search and replace in Muse files

    I have a lot of links to my own files  in a given web page.
    I will be relocating the files and want to know if I can run a search and replace the links with their new urls  on the Muse files
    I do not want to be manually replacing each link using the Muse add link button. That will take forever.
    I will be using a separate search and replace program not anything within Muse
    Clear?

    Hi J KIrby,
    You have the option of 'Find and Replace' in Muse under 'Edit' menu option.
    Also, please refer to our release notes page to see what other features have been added to muse in the last update - Release notes | Adobe Muse CC
    - Abhishek Maurya

  • Search and replace data in excel using a text file

    Hi Scripting Guy:
    I was able to write a script based on all the examples in your blogs, but now I'm stuck:
    I have a text file, which has Server Name, IP Address, Comments
    ABCserver1, 1.1.1.1, remote web server
    DEFserver2, 2.2.2.2, remote app server
    XYZserver3, 3.3.3.3, remote api server
    I have a excel file which has Server Name, IP Address, Protocol, Port. Server Name, and IP Addreess can repeat itself more than once in entire spreadsheet at any location: 
    Server1, x.x.x.x, TCP, 80
    Server2, x.x.x.x, TCP, 80
    Server3, x.x.x.x, TCP, 80
    My script search for the excel spreadsheet for a array that I have defined [Server1,Server2,Server3,...], once the first value matches in spreadsheet it replaces all values of Server1 with first value in text file i.e: ABCServer1 so my new excel sheet looks
    this:
    ABCserver1, x.x.x.x, TCP, 80
    DEFserver2, x.x.x.x, TCP, 80
    XYZserver3, x.x.x.x, TCP, 80
    What I also want is, when I replaces the "Server1" value in spreadsheet, it will also replace the IP Address in next column. This is where I'm stuck I can replace the value of Server Name, but I can't replace the IP address for text file. Remember
    "Server1" can be found at any place in spreadsheet but it will always have IP address column next to it.
    So this is what script will do if runs correctly:
    1) Read text file:
    ABCserver1, 1.1.1.1, remote web server
    DEFserver2, 2.2.2.2, remote app server
    XYZserver3, 3.3.3.3, remote api server
    2) Before script run on excel sheet
    Server1, x.x.x.x, TCP, 80
    Server2, x.x.x.x, TCP, 80
    Server3, x.x.x.x, TCP, 80
    3) After script run on excel sheet
    ABCserver1, 1.1.1.1, TCP, 80
    DEFserver2, 2.2.2.2, TCP, 80
    XYZserver3, 3.3.3.3, TCP, 80
    If you will see my script is able to replace "Server Name" in entire spreadsheet, but can't copy or replace the corespondent IP address in next column. 
    script:
    $text = "Server1","Server2","Server3"
    $replace=$replace = get-content C:\script\test.txt | foreach{ ($_.split(","))[0]}
    $File = "C:\script\test.xlsx"
    $now = [datetime]::now.ToString("yyyy-MM-dd")
    #$now = get-date -Format "MM-dd-yyyy_hh:mm:ss"
    copy-Item C:\script\test.xlsx test_$now.xlsx
    # Setup Excel, open $File and set the the first worksheet
    $i=0
    $Excel = New-Object -ComObject Excel.Application
    $Excel.visible = $true
    $Workbook = $Excel.workbooks.open($file)
    $Worksheets = $Workbooks.worksheets
    $Worksheet = $Workbook.Worksheets.Item(1)
    $Range = $Worksheet.UsedRange
    Foreach($SearchString in $text){
    if ($Range.find("$SearchString")) {
        $Range.replace($SearchString,$replace[$i])
    else {$i++}
    $WorkBook.Save()
    $WorkBook.Close()
    [void]$excel.quit()
    Really appreciate your help
    Thanks

    Hey, thanks for helping me out. I checked online and I had to call Cells this way:
    $Workbook.Worksheets.Item(1).Cells.item($cell.Row,$cell.Column+1)=$test[$i].replace
    one problem I'm having is if the $server value repeats itself in excel sheet again it doesn't replace all values for lets see server 1:
    so thats how my excel sheet is:
    server1
    x
    TCP
    80
    server2
    x
    TCP
    80
    server3
    x
    TCP
    80
    server1
    x
    TCP
    80
    when I run the script thats how results looks:
    server1
    x
    TCP
    80
    REM88888SQL301A
    10.1.1.2
    TCP
    80
    REM88888SQL301B
    10.1.1.3
    TCP
    80
    REM88888SQL301
    10.1.1.1
    TCP
    80
    It skipped the line one on excel and changed value in line 4. As I mentioned above this can repeat $server can repeat itself anywhere in spreadsheet in multiple columns.
    What can we do so it will replace all server name and IP address?
    Thanks

  • Replace text on files using automator

    Hi
    On an older (recently replaced Mac 10.4 etc) I used to be able to use Applescript to change the name of files within a folder very easily.
    Is there an easy way to do the same using Automator?
    This is used a lot during a normal day and would be very helpful. I haven't used Automator much, work keeps getting in the way!!
    Thanks for any help.
    M

    Your Applescript should still work, although it might need some modification.
    I haven't tried these.
    Automator Script to Rename Files
    Automator Script to Rename Files (2)

  • Search and replace, with pattern matching using a table

    I need to inspect a data stream and standardise a set of codes. I need to
    1. Match any patterns with a dash character and remove the dash and any following characters, eg BN-S -> BN, BN-SH -> BN, ARG-22 -> ARG, etc.
    2. Make a few specific word for word replacements, eg, PAEDSH -> PAED
    This is easy to hard code but can it be done using a table of regex substitutions? Can anyone give a pointer or link to some example code? The couple of regex replacement examples I've found use regex to locate but hard code substitutions.
    Thanks

    You could store all your patterns in a Map. Then iterate over the map inserting the patterns into a regex.

  • GUI Based tool for search and replace using regular expression

    Hi,
    I have developed this small tool which can be used for search and replace in multiple files using reqular expressions.
    Features:
    1. Full regular expression
    2. GUI based with Highlighted results
    3. Preview for replace available
    4. Pure Java based.
    5. Its like unix sed and grep
    Please visit below site for download/more information :
    http://sourceforge.net/projects/regexsearchrepl/
    Thanks,
    Hitesh Viseria

    I agree with you, it cannot compete grep/sed/awk combination. I couldnt find anything even close to grep/sed for windows which will also have a preview and most important, free. That is what made me write this tool. I am trying to
    improve its performance and more features like history etc.
    Any suggestions on additional features are most welcome.

  • Search and replace formulas with AppleScript

    Thanks to this forum I've found out that it is possible to search and replace text by other text or formulas with AppleScript in Numbers 3.2
    Does anybody have a script to search and replace a formula or a part of a formula by another formula or a partial formula.
    For example:
    I would like to be able to find:
    = Table1::$b$3
    and replace this by:
    = Table1::$b$4

    De Signature,
    Would this approach work for you...
    This is a very neat way edit a complex formula in a single cell....
    a) Copy and Paste the formula to a new black page in Pages,
    b) Do your Find and Replace, then
    c) Copy and Paste the formulae back into the cell in Numbers.
    It works like a charm.
    Here is an example of how I used this approach...
    Original cell formulae:
    =AVERAGE(
    SMALL(OFFSET(K3,1,0,20,1),1),SMALL(OFFSET(K3,1,0,20,1),2),
    SMALL(OFFSET(K3,1,0,20,1),3),SMALL(OFFSET(K3,1,0,20,1),4),
    SMALL(OFFSET(K3,1,0,20,1),5),SMALL(OFFSET(K3,1,0,20,1),6),
    SMALL(OFFSET(K3,1,0,20,1),7),SMALL(OFFSET(K3,1,0,20,1),8))
    Final version of formulae:
    =AVERAGE(
    SMALL(OFFSET(K3,N1,0,20,1),1),SMALL(OFFSET(K3,N1,0,20,1),2),
    SMALL(OFFSET(K3,N1,0,20,1),3),SMALL(OFFSET(K3,N1,0,20,1),4),
    SMALL(OFFSET(K3,N1,0,20,1),5),SMALL(OFFSET(K3,N1,0,20,1),6),
    SMALL(OFFSET(K3,N1,0,20,1),7),SMALL(OFFSET(K3,N1,0,20,1),8))
    Find Replace: be cautious with the Find, to ensure it is unique, in my this example ",1,", to avoid changing the" ,1)"
    Enjoy...

  • [JS] About Search and replace

    Hello,
    Can anyone tell me how to Search and replace in a document using javascript.
    I want to replace ?? to 5.
    Thank you,
    --Avi

    doc.findTextPreferences.findWhat="??";
    doc.changeTextPreferences.changeTo="5";
    doc.changeText();

  • Search and replace for Unicode characters

    Hello,
    I have a function which searches and replaces characters on file. It works with ASCI sharacters, but not when the strings which needs to be replaced contains Unicode ('á', 'ā') etc.  The source file is codding
    utf8 .
    $file = "file.txt" 
    $SearchReplace = @($file)
    #Process files by performing a search and replace
    foreach ($file in $SearchReplace) 
    #Select-Object -Skip 1 |
        (Get-Content $file) | 
            Foreach-object { $_ -replace 'unicode_string' , ';'   } | 
         out-file -encoding Unicode $file
    How to get working the search (and replace) function with Unicode characters?
    Thanks!

    No. it does not. I have verified that script does not recognize the diacritic
    ('á',
    'ā') characters when at all the operations with the files I have specified encoding utf-8/Unicode.

  • How to search and replace in an xml file using java

    Hi all,
    I am new to java and Xml Programming.
    I have to search and replace a value Suresh with some other name in the below xml file.
    Any help of code in java it is of great help,and its very urgent.
    I am using java swings for generating two text boxes and a button but i am not able to search in the xml file thru the values that are entered into these text boxes.
    Thanks in advance.
    **XML File*
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <student>
    <stud_name>Suresh</stud_name>
    <stud_age>40</stud_age>
    </student>Also i am using SAX Parser in the java program
    any help of code or any tutorials for sax parisng is very urgent please help me to resolve this problem
    Edited by: Karthik84 on Aug 19, 2008 1:45 AM
    Edited by: Karthik84 on Aug 19, 2008 3:15 AM

    Using XPath to locate the elements you are after is very easy.
    Try something like this:
    import java.io.File;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    public class BasicXMLReplaceWithDOM4J {
         static String inputFile = "C:/student.xml";
         static String outputFile = "C:/studentRenamed.xml";
         public static void main(String[] args) throws Exception {
              // Read xml and build a DOM document
              Document doc = DocumentBuilderFactory.newInstance()
                        .newDocumentBuilder().parse(new InputSource(inputFile));
              // Use XPath to find all nodes where student is named 'Suresh'
              XPath xpath = XPathFactory.newInstance().newXPath();
              NodeList nodes = (NodeList)xpath
                   .evaluate("//stud_name[text()='Suresh']", doc, XPathConstants.NODESET);
              // Rename these nodes
              for (int idx = 0; idx < nodes.getLength(); idx++) {
                   nodes.item(idx).setTextContent("Suresh-Renamed");
              // Write the DOM document to the file
              Transformer xformer = TransformerFactory.newInstance().newTransformer();
              xformer.transform(new DOMSource(doc), new StreamResult(new File(outputFile)));
    }- Roy

  • Using a variable in a Powershell search and replace string

    Hi
    a couple of days ago I posted a question about doing a search and replace with wildcards
    Search and repalce with Widcards
    I got a swift and very helpful answer but now I need to build on it.
    In a text file I wanted to replace all the text between two defined words.  the script I got was this
    $text = 'Some Server this bit of text varies Language stuff'
    $text -replace '(.*Server) .+? (Language.*)','$1 it will always say this $2'
    It works great but now I want to replace "it will always say this" with a variable and I can't figure out the correct grammar to make this happen.
    Can anyone help??
    Thanks
    Alex

    Here's one way:
    $replace = 'it will aways say this'if ( $text -match '(.*Server) .+? (Language.*)' )
    { "{0} $Replace {1}" -f $matches[1,2] }
    else { $text }
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

  • Search .docx and replace text with image

    I've got a directory containing a series of images. The images will always be the same name and I need to insert them into placeholders in a Word document which will be a template. I thought of using the image names as placeholders, opening the document
    and searching for the image name, replacing it by inserting the image, and doing so for each image in the directory.
    $file is the name of the image in the directory and it loops through them okay.
    foreach($file in Get-ChildItem $savepath -Filter *.jpg)
            # search word doc and replace selected text with image ($file)    
    Also inserting the image seems simple enough from a TechNet article I found, but I've got no idea how to open the Word document and do a search and replace. I found a few articles related to the subject but I couldn't get them to work when I tried to adapt
    them.
    Any help is appreciated. Thanks in advance.

    This 'might' be possible, but I'm having a hard time finding good references to the com object capabilities for inserting an image into a word document.  Creating new, converting format, that sort of thing is straightforward.
    I'd do a search on "powershell word comobject" and variations of insert image update edit, etc.  Or maybe someone else with more experience/knowledge has a magic bullet for you.  Once you get some info on doing it with powershell, expand
    your search by omitting the powershell keyword, there's gotta be some solid documentation for the comobject somwhere, but it will probably be a bit complex.
    You can also:
    $word = new-object -comobject word.application
    $doc = $word.documents.add("<path to word document>"
    and get-member to your heart's content, but finding references and/or documentation might be easier.  
    Good luck!
    Edit:  This could help, but really doesn't give much insight into placement of the image, only helps getting the image into the doc:  http://gallery.technet.microsoft.com/office/44ffc6c8-131f-42f1-b24b-ff92230b2e0a
    If you do find something useful, post it here, I'm sure others could benefit!
    SubEdit:  Should have thought of this already...
    http://msdn.microsoft.com/en-us/library/ff837519(v=office.14).aspx

Maybe you are looking for

  • Does it need effort to upgrade SSRS from SQL Server 2005 to 2014?

    Hi, i have a customer who wants to upgrade the their system. for SQL Server part, the current version is 2005, they want to upgrade to 2014. is it possible that don't change any code for SSRS? if have to change, what's the estimated effort? the total

  • MIRO - Tax on sales.

    Hi experts, I´m trying to make a "MIRO" "Invoice Verification" from a Purchase Order. I want to change Tax on sales by item of purchase order when the material is 'x' with badi or user exits . Can anybody help me ? Thanks in advanced.

  • Execute in background

    Hi! I have a program after 20-30 minutes die for Run Time Exceded. The program consists in a selection screen and then we call a function. I have thought to call this function in background. CALL FUNCTION 'ZBAPI_00001' IN BACKGROUND TASK But the prog

  • ETL process

    hi all, I am new to OWB10g. I need to know what is exact ETL process to follow in OWB. And how to do the incremental loading. please tell me the Steps to follow??

  • Turning off copy and paste settings

    If I previously copy and paste settings in lightroom 5 from one photo to the next as I scroll through my photos it will continue to copy and paste the settings without being prompted. How do I turn this off?