Powershell Download text file in real format

Hi i am trying to download a text file using Powershell Invoke-WebRequest method

Invoke-WebRequest -Outfile DL_script.txt

content of script.txt:

cls
@echo off ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%" ECHO args = "ELEV " >> "%vbsGetPrivileges%" ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%" ECHO args = args ^& strArg ^& " " >> "%vbsGetPrivileges%" ECHO Next >> "%vbsGetPrivileges%" if '%cmdInvoke%'=='1' goto InvokeCmd ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%" goto ExecElevation

but after downloading.. file looks like this (All Code in Single line, no new line) how can i download file in real format so i can run script

cls@echo offECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"ECHO args = "ELEV " >> "%vbsGetPrivileges%"ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"ECHO args = args ^& strArg ^& " " >> "%vbsGetPrivileges%"ECHO Next >> "%vbsGetPrivileges%" if '%cmdInvoke%'=='1' goto InvokeCmd ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
goto ExecElevation

i need a single line command for download script in real format. tried with these methods but same output (everything in single line)

# Method 1:
Invoke-WebRequest $url -OutFile $path_to_file
# Method 2:
(New-Object System.Net.WebClient).DownloadFile($url, $path_to_file)
# Method 3 (requires BITS may not be immediate):
Import-Module BitsTransfer
Start-BitsTransfer -Source $url -Destination $path_to_file
1

1 Answer

As for this...

i need a single line command for download script

... ditto to what Ramhound stated, but in PowerShell, there is a difference between a one-liner (pipelined) command...

SendCommand1 | ReceiveCommand...

...,and commands in the pipe must support pipelined input, then there is all code on one line.

SomeCodedCommand;SomeOtherCodedCommand

The semi-colon means each block is independent of the other.

Chapter 4 - One-liners and the pipeline

What’s a PowerShell One-Liner & NOT a PowerShell One-Liner?

How your file is rendered on the download can be related to who the source delivers it, not necessarily something to do with PowerShell.

One way to determine this is not to do the download and just check what the website page is delivering. Hit the site, using your browser developer tools, and see what is there.

Since we'd not have access to the file you are using (which looks very suspicious, btw) via the site you are showing, you should try another site, and text file for validation.

For example, this site's text file comes down as expected on one of my isolated test systems.

$url = '
Invoke-WebRequest $url -OutFile 'C:\Temp\Week-of-Mon-20020408.txt'
Get-Content -Path 'C:\Temp\Week-of-Mon-20020408.txt'

Or this sample one...

$url = '
Invoke-WebRequest $url -OutFile 'C:\Temp\sample1.txt'
Get-Content -Path 'C:\Temp\sample1.txt'
6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like