{"id":97,"date":"2007-02-13T12:08:00","date_gmt":"2007-02-13T16:08:00","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/2007\/02\/13\/powershell-get-content\/"},"modified":"2013-07-02T08:11:42","modified_gmt":"2013-07-02T12:11:42","slug":"powershell-get-content","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/","title":{"rendered":"PowerShell Get Content"},"content":{"rendered":"<p>One slick trick you can do in PowerShell with text files that's difficult to accomplish without extra tools is to display sections of text files without showing the entire file.  When you use the Get-Content cmdlet, the resulting content is treated as an array of strings. This means you can simply specify the line you want displayed like this:<\/p>\n<p><span style=\"font-family:Lucida Console;font-size:85%;color:#0000ff;\">(get-content c:\\boot.ini)[0]<\/span><\/p>\n<p>Don't forget that arrays start counting at 0. If you want to see a range of lines, simply specify the range:<\/p>\n<p><span style=\"font-family:Lucida Console;font-size:85%;color:#0000ff;\">(get-content c:\\boot.ini)[0..5]<\/span><\/p>\n<p>This will display the first 6 lines of c:\\boot.ini.  Want to look at the last X number of lines? Well you first need to get the number of lines.<\/p>\n<p><span style=\"font-family:Lucida Console;font-size:85%;color:#0000ff;\">(get-content c:\\boot.ini).Length<\/span><\/p>\n<p>Once you know the length, specify whatever range you want.  My boot.ini is 7 lines long so if I wanted to see the last 3 lines I would simply run<\/p>\n<p><span style=\"font-family:Lucida Console;font-size:85%;color:#0000ff;\">(get-content c:\\boot.ini)[5..7]<\/span><\/p>\n<p>Of course, this little tidbit wouldn't be complete without a one-liner to get the last 5 lines without knowing in advance how long the file is:<\/p>\n<p><span style=\"font-family:Lucida Console;font-size:85%;color:#0000ff;\">$c=get-content c:\\boot.ini;$l=$c.length;$c[($l-5)..$l]<\/span><\/p>\n<p>If you specify a number greater than the value of $l then the entire file will be returned.<\/p>\n<p>Obviously the boot.ini file is hardly an exciting thing to test so try this out on log files on your systems. <\/p>\n<p>These commands are very similar to the Head and Tail utilities and in fact you could easily turn these into your own Head and Tail functions.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:a38668ed-06c6-48bc-98cb-c15fbead0c82\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline;\"><span style=\"font-size:85%;\">Technorati tags: <a href=\"http:\/\/technorati.com\/tags\/PowerShell\" rel=\"tag\">PowerShell<\/a>, <a href=\"http:\/\/technorati.com\/tags\/Scripting\" rel=\"tag\">Scripting<\/a>, GetContent, <a href=\"http:\/\/technorati.com\/tags\/cmdlet\" rel=\"tag\">cmdlet<\/a><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>One slick trick you can do in PowerShell with text files that&#8217;s difficult to accomplish without extra tools is to display sections of text files without showing the entire file. When you use the Get-Content cmdlet, the resulting content is treated as an array of strings. This means you can simply specify the line you&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[28],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-cmdlet"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PowerShell Get Content &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PowerShell Get Content &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"One slick trick you can do in PowerShell with text files that&#039;s difficult to accomplish without extra tools is to display sections of text files without showing the entire file. When you use the Get-Content cmdlet, the resulting content is treated as an array of strings. This means you can simply specify the line you...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2007-02-13T16:08:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-07-02T12:11:42+00:00\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/97\\\/powershell-get-content\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/97\\\/powershell-get-content\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"PowerShell Get Content\",\"datePublished\":\"2007-02-13T16:08:00+00:00\",\"dateModified\":\"2013-07-02T12:11:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/97\\\/powershell-get-content\\\/\"},\"wordCount\":269,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"keywords\":[\"cmdlet\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/97\\\/powershell-get-content\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/97\\\/powershell-get-content\\\/\",\"name\":\"PowerShell Get Content &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"datePublished\":\"2007-02-13T16:08:00+00:00\",\"dateModified\":\"2013-07-02T12:11:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/97\\\/powershell-get-content\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/97\\\/powershell-get-content\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/97\\\/powershell-get-content\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PowerShell Get Content\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PowerShell Get Content &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/","og_locale":"en_US","og_type":"article","og_title":"PowerShell Get Content &#8226; The Lonely Administrator","og_description":"One slick trick you can do in PowerShell with text files that's difficult to accomplish without extra tools is to display sections of text files without showing the entire file. When you use the Get-Content cmdlet, the resulting content is treated as an array of strings. This means you can simply specify the line you...","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/","og_site_name":"The Lonely Administrator","article_published_time":"2007-02-13T16:08:00+00:00","article_modified_time":"2013-07-02T12:11:42+00:00","author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"PowerShell Get Content","datePublished":"2007-02-13T16:08:00+00:00","dateModified":"2013-07-02T12:11:42+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/"},"wordCount":269,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"keywords":["cmdlet"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/","name":"PowerShell Get Content &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"datePublished":"2007-02-13T16:08:00+00:00","dateModified":"2013-07-02T12:11:42+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/97\/powershell-get-content\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"PowerShell Get Content"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":98,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/98\/more-fun-with-get-content\/","url_meta":{"origin":97,"position":0},"title":"More Fun with Get-Content","author":"Jeffery Hicks","date":"February 15, 2007","format":false,"excerpt":"A few followups on my recent post about Get-Content. First, you can also use the CAT alias if you're used to the Unix world, or TYPE in place of get-content. The Get-Content cmdlet does have a parameter called -totalcount that will return the specified number of lines from the beginning\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":88,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/88\/powershell-parsing\/","url_meta":{"origin":97,"position":1},"title":"Powershell Parsing","author":"Jeffery Hicks","date":"January 16, 2007","format":false,"excerpt":"In PowerShell, Get-WMIObject is a terrific cmdlet for remotely managing systems. If you have a text list of server or computer names, here's a quick method you could enumerate that list and do something to each server.foreach ($server in (Get-Content s:\\servers.txt)) {#skip blank lines if (($server).length -gt 0) { $server\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":445,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/445\/more-fun-with-get-numberedcontent\/","url_meta":{"origin":97,"position":2},"title":"More Fun with Get-NumberedContent","author":"Jeffery Hicks","date":"October 13, 2009","format":false,"excerpt":"As much fun as the original Get-NumberedContent function was after using it for awhile I realized I had imposed some limitations. I also realized it needed to be more flexible. What if someone wanted to specify a different color or use a different comment character such as a ; in\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"Help Get-NumberedContent","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2009\/10\/captured_Image2.png_thumb2.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3685,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3685\/friday-fun-the-measure-of-a-file\/","url_meta":{"origin":97,"position":3},"title":"Friday Fun: The Measure of a File","author":"Jeffery Hicks","date":"February 14, 2014","format":false,"excerpt":"I've been working with PowerShell since the days of Monad and have written a lot of PowerShell scripts. Out of idle curiosity, and the need for a Friday Fun topic, I decided to see how many lines of PowerShell I have written. Or at least that are in my Scripts\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"ruler","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/02\/ruler-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":35,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/35\/printing-from-powershell\/","url_meta":{"origin":97,"position":4},"title":"Printing from PowerShell","author":"Jeffery Hicks","date":"June 6, 2006","format":false,"excerpt":"PowerShell has a slick feature that allows you to send the output from a cmdlet or expression directly to a printer. Pipe the output to the Out-Printer cmdlet and it will print out on the default printer:get-process | out-printer If you have other printers installed you can use the printer\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4000,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4000\/using-optimized-text-files-in-powershell\/","url_meta":{"origin":97,"position":5},"title":"Using Optimized Text Files in PowerShell","author":"Jeffery Hicks","date":"September 8, 2014","format":false,"excerpt":"If you are like many IT Pros that I know, you often rely on text files in your PowerShell work. How many times have you used a text file of computernames with Get-Content and then piped to other PowerShell commands only to have errors. Text files are convenient, but often\u2026","rel":"","context":"In &quot;CommandLine&quot;","block_context":{"text":"CommandLine","link":"https:\/\/jdhitsolutions.com\/blog\/category\/commandline\/"},"img":{"alt_text":"document","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2014\/09\/document.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=97"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/97\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}