-2

The password for Century10 is the 161st word within the file on the desktop.

NOTE: - The password will be lowercase no matter how it appears on the screen.

*The question above is where i am facing my challenges. I tried the command below. *

Get-Content C:\Users\Century9\Desktop\Word_File.txt | Select-Object -Index 161

Result was nil. I understand that i need to assign a value to the string as it is now seen as one whole entity. But how do i do it ?

mklement0
  • 382,024
  • 64
  • 607
  • 775

2 Answers2

1

If the token of interest is the 161st word in the file, use the following approach, which splits the file into words irrespective of line breaks[1]:

$pass = (-split (Get-Content -Raw Word_File.txt))[160]

Append .ToLower() if you want to convert the token to all-lowercase.

Note that the above loads the entire file into memory as a single string, using -Raw.

  • Since array indices are 0-based, it is index [160] that returns the 161st element.

  • The unary form of the -split operator splits the input into an array of tokens by whitespace.

    • Note: If you want to split by the stricter definition of what constitutes a word in a regular-expression context, use the following instead:
$pass = ((Get-Content -Raw Word_File.txt) -split '\W+' -ne '')[160]

[1] If your input file contains each word on its own line:

Your solution was on the right track, except that you should pass 160 to Select-Object -Index, because the -Index parameter expects 0-based indices, not 1-based line numbers:

# Extract the 161st line.
$pass = Get-Content Word_File.txt | Select-Object -Index 160

To convert to lowercase:

$pass = (Get-Content Word_File.txt | Select-Object -Index 160).ToLower()

The above will fail if the input file has fewer than 161 lines (with error message You cannot call a method on a null-valued expression).

If you prefer to receive no output quietly instead, use the following (which uses built-in aliases select for Select-Object and foreach for ForEach-Object for brevity):

$pass = Get-Content Word_File.txt | select -Index 160 | foreach ToLower
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • any idea on where i should start to master powershell and learn scripting ? I tried my research and underthewire website appears to be one of the website which turn up upon searching. Thank you nevertheless for your answers :) –  Oct 24 '19 at 13:20
  • @JellyRainbows: https://stackoverflow.com/a/48491292/45375 contains some pointers to resources for getting started. Also, allow me to give you the standard advice to newcomers in the next comment. – mklement0 Oct 24 '19 at 13:27
0

Try running this:

((Get-Content -Path C:\Users\Century9\Desktop\Word_File.txt -TotalCount 161)[-1]).ToLower()
xyz
  • 393
  • 2
  • 12
  • Method invocation failed because [System.Char] does not contain a method named 'ToLower'. At line:1 char:1 + ((Get-Content -Path C:\Users\Century9\Desktop\Word_File.txt -TotalCou ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound –  Oct 17 '19 at 14:01
  • @JellyRainbows: That would only happen if you used `1` with `-TotalCount`; to avoid that problem, use `@(...)` rather than `(...)` around the `Get-Content` command. – mklement0 Oct 17 '19 at 14:21