PowerShell Live - being retired

Please go to http://www.shelltools.net for more information

Welcome to PowerShell Live - being retired Sign in | Join | Help
in
Home Main Site Blogs Forums Videos Chat Customer Support

reference

  • get-content

    Aliases: gc, cat, type

    Purpose: Retrieving item content

    Examples:

    Getting all content in a text file by specifying the files path:

    get-content c:\mytext.txt

    ${c:\mytext.txt}

    Note: $ { } main purpose is to allow access to identifier names that would otherwise cause a conflict because of special characters. That's why inside the curly brackets only literals are allowed which are treated as-is. You must type in the text exactly the way it is going to be used. You may not quote it, and you may not use variables instead because in these cases, get-content would actually look for a quoted path (which does not exist) or a path named like the variable you used (which also does not exist). That's why $ { } really is no alternative to get-content in most cases. 

    Getting the content of a file from a FileInfo object:

    $file = Dir c:\mytext.txt

    $file | get-content

    Reading only the first 10 lines of a text based file:

    get-content c:\mytext.txt -totalcount 10

    Getting line 5 from a text based file:

    $content = get-content c:\mytext.txt

    $content[4]

    Or:

    (get-content c:\mytext.txt)[4]

    Getting lines 4 to 7:

    (get-content c:\mytext.txt)[3..6]

    Getting the last 3 lines from a text based file:

    (get-content c:\mytext.txt)[((get-content c:\mytext.txt).length-3)..((get-content c:\mytext.txt).length)]

    Processing content of a text based file line by line:

    get-content c:\mytext.txt | foreach-object { "Processing: " + $_ }

    get-content c:\mytext.txt | % { "Processing: " + $_ }

    get-content c:\mytext.txt | %{ $a++; "Zeile $a = $_"}

    Retrieving all lines of a text based file that contain a special keyword:

    get-content $env:windir\windowsupdate.log | select-string "Agent"

    get-content $env:windir\windowsupdate.log | select-string "`tAgent`t"

    Reading in a text based file with server names or IP addresses and use that to connect to and retrieve remote information:

    Get-Content "servers.txt" | %{ get-wmiobject win32_operatingsystem -computername $_ } | export-csv "result.csv" –notypeinformation 

    Determine number of lines in a text document:

    get-content c:\mytext.txt | measure-object 

    (get-content c:\mytext.txt | measure-object ).Count

    (get-content c:\mytext.txt).Length

    Reading binary content:

    get-content $env:windir\explorer.exe -encoding byte

    Reading binary content in chunks of 10 bytes:

    get-content $env:windir\explorer.exe -encoding byte -readcount 10 | %

    {

    $line = $_

    $text = $line | %{ " " + ("{0:x}" -f $_).PadLeft(2,"0")}

    "$text"

    }

    Reading 4 bytes from a specific position:

    $file = "$env:windir\explorer.exe"

    Get-Content -encoding byte $file -read 4 -total 4

    Read the code of a function:

    Dir function:

    Get-Content function:more

    $function:more

    Get-Content function:Z:

    $function:Z:

     

     

This Blog

Syndication

Recent Posts

Archives