1

How can I take the name of the current folder and put into a variable in powerShell?

Thanks.

John Adam1
  • 55
  • 1
  • 6

2 Answers2

6

you can get the current working directory with

pwd 

(pwd is an alias for Get-Location)

if you then select this as an object you can get the path

pwd | Select-Object | %{$_.ProviderPath}

you can split this

pwd | Select-Object | %{$_.ProviderPath.Split("\")}

Then take the last - which is you folder name

pwd | Select-Object | %{$_.ProviderPath.Split("\")[-1]}

to assign to a variable

$folderName = pwd | Select-Object | %{$_.ProviderPath.Split("\")[-1]}
developer
  • 690
  • 7
  • 16
2

to return your current location use Get-Location

physlexic
  • 826
  • 2
  • 9
  • 21