3

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.

6
  • 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 Commented Jun 27, 2017 at 3: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? Commented Jun 27, 2017 at 3:25
  • Possible duplicate of How to print a certain line of a file with PowerShell? Commented Jun 27, 2017 at 3:40
  • And your logic should work. In my local , I tested ,there is no issues . Although you can try the index logic Commented Jun 27, 2017 at 3:52
  • @DavidC.Rankin I tried but it does not work. I keep giving me an error of "Cannot find path". Commented Jun 27, 2017 at 5:04

4 Answers 4

6
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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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.
I think you can refer to this maybe this can solve the problem stackoverflow.com/questions/18847145/…
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] 

2 Comments

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: Yup. I second you. Its just one more way. :)
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] 

2 Comments

$PSScriptRoot?
$PSScriptRoot war introduced in Powershell v3 IIRC, this was the previous method which still works on all supported Microsoft OSes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.