-4

I need to use a .txt as an input for a powershell command, and I need it to output to the same file in new row.
What kind of commands should i use?
I've only tried basic stuff such as

C:\"bunch O'shit"\input.txt *2   

"C:\"bunch O'shit"\input.txt" *2  

Neither of which have worked.
Hlp plz.

1 Answers1

0

You need first to read the content of your file before you can start processing it. To read a file using PowerShell, you can use the Get-Contentcmdlet.

Once you got the content of your file, you need to convert it to a type that you can use for doing calculations, i.e. you need to do a type-cast.

Type casting you can do like this, assuming the variable $num is a string:

[int]$num

this will create an integer out of a string.

You can check the type of a variable by using the Get-Membercmdlet.

Putting all this together:

# For the sake of it, let's create a file containing a single number
"2"  | Out-File -FilePath .\number.txt

# Let's read the file we just created and save the result in a variable
$num = Get-Content .\number.txt

# Do the type cast and multiply the result with another number
[int]$num * 2

You can at any point pipe the result to Get-Member and check what type it is.

kim
  • 3,385
  • 2
  • 15
  • 21