2

I am trying to write a powershell script which will return the contents inside a txt file. However, I do not want to specify the drive of the txt file as the txt file will be placed in the same folder as the powershell script.

I am using this line of code:

get-content .\document.txt | select -first 1 -skip 1

but it doesnt work.


Inside document.txt:

  • This is the first line
  • This is the second line

What script do I write to retrieve the second line "This is the second line" without having to put the full path like "C:\Data\Scripts\Document.txt"? I have searched online solution but many solutions required me having to put its destination path.

thompsonrapier
  • 155
  • 3
  • 16
  • Is there any environmental variable where the path is mapped? If you do not wish to give the filename then how PS will come to know which file to read. By some measures , you have to specify the filepath. You can create a env variable and give the path. Later from the script you can read that path and mention the specific file name. In that way, there is no hardcoding of the filepath inside the script – Ranadip Dutta Jun 27 '17 at 03:20
  • Hi, I just want powershell to search for the file "document.txt" in the same folder. I do not want to state D:\Data\Scripts\document.txt in the script. Is it possible to search for document.txt in the "Scripts" folder? – thompsonrapier Jun 27 '17 at 03:25
  • Possible duplicate of [How to print a certain line of a file with PowerShell?](https://stackoverflow.com/questions/14759649/how-to-print-a-certain-line-of-a-file-with-powershell) – David C. Rankin Jun 27 '17 at 03:40
  • And your logic should work. In my local , I tested ,there is no issues . Although you can try the index logic – Ranadip Dutta Jun 27 '17 at 03:52
  • @DavidC.Rankin I tried but it does not work. I keep giving me an error of "Cannot find path". – thompsonrapier Jun 27 '17 at 05:04
  • @thompsonrapier there are two additional methods listed in the line `select index` and `(...)[2]`, both failed? If you still have questions, edit your question and add what it is you typed -- exactly. That will help us help you. – David C. Rankin Jun 27 '17 at 06:36

4 Answers4

5
get-content $PSScriptRoot\document.txt | select -first 1 -skip 1

Note that $PSScriptRoot will only have a value when the script is executing, but it will represent the path of where the script resides.

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
0

I may not understand question correctly but you can display the content of the text file by

cat <yourfile.name>

if you want to get the part of the text file you can do

cat <yourfile.name> | Select-String '<text you want to get>'

you can refer to this site on how to manipulate the output https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/select-string

and if ever you want to run the script in Bash you need different script if ever

  • Im trying to call the txt file and read the second line. However, I wish the script to automatically find the txt file inside the same folder and return the 2nd line in the txt file. – thompsonrapier Jun 27 '17 at 03:27
  • I think you can refer to this maybe this can solve the problem https://stackoverflow.com/questions/18847145/loop-through-files-in-a-directory-using-powershell – John Michael Villegas Jun 27 '17 at 03:36
0

TO find the text file have a look at this link PowerShell: current directory relative to executing script

After you get that ,We can move to second line of the file easily,

Solution here:-to print the required line

The code may convert itself to be something like this

#include this is in your script,this part of the code will get the absolute path of the script from whereever its running
function Get-ScriptDirectory
    {
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    Split-Path $Invocation.MyCommand.Path
    }

$path = Join-Path (Get-ScriptDirectory) 'Your Script Name'
$path.ToString()
$path =$path.Substring(0,$path.IndexOf('Your Script Name'))
$path
#now $path has the directory in which you have your files,append it with your text file name 

(Get-content $path+'textfilename.txt')[1]
#and get the second line of the content

Credits to SomeShinyobjects

This can be simply written as

$path=$PSScriptRoot+'\textfilename.txt'
(Get-content $path)[1]
Chetan Kulkarni
  • 404
  • 4
  • 15
0

If you are looking for the second line then use the below: By default, each line will be an index of an array. So the second line will be having the index as 1

(get-content .\document.txt)[1]
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • Depending on the size of the document this would be a performance hog since it has to read the whole file first. Other options are more friendly... at least in more current versions of PS. – Matt Jun 27 '17 at 12:43
  • @Matt: Yup. I second you. Its just one more way. :) – Ranadip Dutta Jun 28 '17 at 03:37