Monday, February 6, 2012

Part-4: Text Manipulation in PowerShell using .Replace(), and .Remove() methods.

Part-1: Text Manipulation in PowerShell using Trim, TrimStart , TrimEnd methods
Part-2: Text Manipulation in PowerShell using .ToLower and .ToUpper method
Part-3: Text Manipulation in PowerShell using .StartsWith() and EndsWith() methods
Today we are using .replace and .remove methods to play with the string. Lets start. Today i am using some fake email id as text.
$text =
06-02-2012 12-48-52
Using replace we can replace the text,symbols of our strings.  The syntax is simple Replace(“Old Character”,”new character”)
First lets replace “Aman” with some other name , for example with “SAM”,"
You can see in below screenshot that  text “” is replaced with “

06-02-2012 12-52-28 
Secondly lets replace “.” with a blank spaces
$text.Replace("."," ")
In the screenshot you can se that all “.” are replaced with the “ “ a black space.
06-02-2012 12-55-42
lets assume that we have a text file which contain list of all users email id in our “XYZ” company and we want to remove “” and keep the text before “” .
$text.Replace(""," ")
and here is the our output :) ,showing only the text before “
06-02-2012 13-01-11
Before doing anything else first lets check the length of our text using Length property. The syntax for remove method is to define to keep the number of characters and remove everything else. Not getting my point.. let ,me show to you.
Our text have 19 characters in it.

06-02-2012 13-21-38 
You can see that i mentioned 4 characters to keep and remove everything else. and the output is “Aman” which are exactly 4 characters.
06-02-2012 13-22-55
Now i am keeping 11 characters of the string and removing everything else. and you can see that the output is  “Aman.Dhally” which are exactly 11 characters .
06-02-2012 13-25-15
there is another example of Remove() method. 
In this example. first you can see our original text “”, in second we are keeping the first 4 characters of the text and removing everything else, so the output is “Aman”, in third we are keeping first 4 characters of the text "which is “AMAN”, and then removing next 7 characters of the text which is “.Dhally”  and then the output is “

06-02-2012 13-34-04


Thanks for reading
Aman Dhally


  1. Hello how can i remove the last 2 digits of my string:

    [string]$text = "Archive_Testebene1_20120228_124556"


    With remove it only remove the first 4

    1. Hi Jan
      To remove the last 2 digits try

    2. It is not working. Please tell how to remove some starting characters

    3. $text.substring(2) will remove the first 2 characters

  2. Hi...I have one problem..Maybe you could help me :)
    I have a folder with PDF files in this format...
    date_number_email.pdf where date=8 digits , number = 13 digits
    I have to create two CSV files...
    1. files.csv with sheet files with two columns, A and B where column A will contain date_email.pdf and column B will contain number
    2. files.csv with sheet files with two columns, A and B where column A will contain date_email.pdf and column B will contain email

    Thanks in advance


    1. Hi Alex,

      I think that can be done {not sure} , can you give me few sample file names.


    2. [string[]]$pdfFileNames = @"
      "@ -split "`n"

      $pdfObject = foreach ($pdfFileName in $pdfFileNames)
      $fileParts = $pdfFileName.Split("_",3)
      New-Object -Type PSObject -Property @{
      Date = $fileParts[0]
      Number = $fileParts[1]
      Email = $fileParts[2]

      $pdfObject |
      Select-Object -Property @{Name = "Date_Email";
      Expression = {$_.Date + "_" + $_.Email}},Number |
      Export-Csv -Path C:\Users\Owner\Tmp\File1.csv

      $pdfObject |
      Select-Object -Property @{Name = "Date_Email";
      Expression = {$_.Date + "_" + $_.Email}},Email |
      Export-Csv -Path C:\Users\Owner\Tmp\File2.csv

  3. Hi Aman,

    Thanks for the reply...

    I think this is all you need to create the csv files.
    This is done by command dir /b (I am sure you know :) )


  4. Hi Aman,
    Great work and it answered some questions that I had. I am trying to create a script that will do the following. Reads data from a CSV specifically the teachnam column. The field consists of the users full last name, and the first initial of there first name. It will split this data and combine them to be first initial and last name combined. The longest this field should be is 6 characters. It can be shorter but it cannot be longer. is an example of the csv.

    Smith, J,Algebra II,3,J210
    Johnson, A,Earth Science,2,S103

    I am looking for the script to return
    Smith, J as JSmith
    Johnson, A as AJohns

    Here is what I have for code, and I am recieving errors about jsmith not being the correct length.

    ForEach ($entry In $CSV)
    #This line reads the teachname field from the CSV and removes the #space
    $teachname = $entry.teachname -replace '\s+', ""

    #This line splits the name up
    $TeachSplit = $TeacherName.split(",")

    #This line rearranges in correct order
    $TeachFull = $TeachSplit[1] + $teachSplit[0]

    #These next 2 lines Read the length and remove everything after the #6th character
    $TeacherID = $TeachFull.Remove(6)
    Any help would be great.

    1. Hi Russ,

      Thanks . can you try this .

      $csv = Import-Csv D:\D.csv -Delimiter ","

      foreach ( $name in $csv ) {

      $myName = $name.ccoursenam + $name.teachname



  5. Hi Aman i stuck with a problem let's say i have a file with almost 50 lines in it every lines have colon inside a phrase i need to delete or remove all the lines after the colon, ie
    abcd : aajjnnnaa
    ab: qwsxx
    mmmjkkkoo : lopppkk has to be


  6. Hi,

    I need to add a word in a text file. I have bunch of lines in that text file and somewhere in the middle, i have a line like this and i need one more server name at end of the line.

    $new - "Server10"

    $Watchers = @( "Server1","Server2","Server3")

    Please suggest me the script.

  7. Hi Aman
    I have a column with firstnames and lastnames from a csv file
    the firstname column has multiple firstnames.
    How would i construct the Remove function so that it removes all other words after the first space.
    E.g. my firsname is Amy May Zan. and the last name is Hakim.
    How do i just make it

  8. Thanks, great post. Helped me out :)

  9. Hi Aman, I would like to do following.

    20141001-2568-C2568-TRX (L)-169082523u.xml rename this file to ABN-2568-C2568-TRX.xml
    I've 7 files under one folder with similar naming convention.

    let me know best way to do this.

    Thank you.

  10. hi Aman can u help me with the syntax to replace double quote with /"

  11. Hello

    How can I get the first 7 letters of a string? The strings can vary in size from 2 to 15 characters long.

  12. This comment has been removed by the author.