0

In this case, how can I exclude the file path part of this line?

Link:

www.website.com/downloads/file.exe

Can be .exe, .zip, etc.

Output Goal:

file.exe

Also want to use it as so:

$x = file.exe

$path = C:\Users\Downloads\$x

How can this be pulled of using powershell?

dannyr.gez
  • 77
  • 1
  • 6
  • 1
    It might help you to read [ask]. – Enigmativity Feb 22 '21 at 00:47
  • 1
    Probably the most convenient way here is to use .Net [System.IO.Path](https://learn.microsoft.com/en-us/dotnet/api/system.io.path) class for this: `$fileName = [System.IO.Path]::GetFileName('www.website.com/downloads/file.exe') # --> file.exe` – Theo Feb 22 '21 at 10:33

2 Answers2

2

You can use the split-path cmdlet with the parameter -leaf:

'www.website.com/downloads/file.exe' | Split-Path -leaf

Will have an output of file.exe

CraftyB
  • 721
  • 5
  • 11
0

I would recommend you to search a bit more before asking.
I did easily find the solution online in 5 minutes.

$link = 'www.website.com/downloads/file.exe'

$lastSlashPos = $link.LastIndexOf('/')
$fileNameWithExtension = $link.Substring($lastSlashPos+1)

Articles:

Stefano Paviot
  • 130
  • 1
  • 6