Menu
Say you have the variable
$source = 'C:temponetwothree' and you want to set $destination equal to $destination = 'C:temponetwo' programmatically, how might you do it?
The best idea I've had would be to trim that, but is there a better way?
Perhaps something like
TomTom
1,30955 gold badges2222 silver badges3939 bronze badges
2 Answers
Getting the parent from a DirectoryInfo object, like Lee suggests, will certainly work.Alternately, if you prefer to use more PowerShellesque commands as opposed to direct .NET Framework calls, you can use PowerShell's built-in Split-Path cmdlet like this:
mikekolmikekol
EDIT: You can get a LeeLee
DirectoryInfo for a directory using get-item so you can instead do:
123k1515 gold badges181181 silver badges251251 bronze badges
Not the answer you're looking for? Browse other questions tagged powershell or ask your own question.
My PowerShell prompt's currently pointed to my C drive (PS C:>). How do I Change Directory to a folder in my Q (PS Q:>) drive? Folder name in my Q drive is 'My Test Folder'.
SoConfused
SoConfusedSoConfused
60622 gold badges66 silver badges1717 bronze badges
5 Answers
Unlike the CMD.EXE
CHDIR or CD command, the PowerShell Set-Location cmdlet will change drive and directory, both. Get-Help Set-Location -Full will get you more detailed information on Set-Location , but the basic usage would be
By default,
CD in PowerShell is an alias for Set-Location .
(Asad reminded me in the comments that if the path contains spaces, it must be enclosed in quotes.)
Jeff ZeitlinJeff Zeitlin
5,38511 gold badge1111 silver badges2828 bronze badges
To go directly to that folder, you can use the BenHBenH
Set-Location cmdlet or cd alias:
7,77511 gold badge1010 silver badges2727 bronze badges
Multiple posted answer here, but probably this can help who is newly using PowerShell
SO if any space is there in your directory path do not forgot to add double inverted commas '.
wonea
2,3461515 gold badges6565 silver badges123123 bronze badges
DeepeshDeepesh
Set-Location -Path 'Q:MyDir' In PowerShell cd = Set-Location
Rao AdnanRao Adnan
You can simply type
Q: and that should solve your problem.
Cordo van SaviourCordo van Saviour
Not the answer you're looking for? Browse other questions tagged powershell or ask your own question.
I'm using PowerShell 2.0 and I want to pipe out all the subdirectories of a certain path. The following command outputs all files and directories, but I can't figure out how to filter out the files.
I've tried using Anthony Mastrean
$_.Attributes to get the attributes but then I don't know how to construct a literal instance of System.IO.FileAttributes to compare it to. In cmd.exe it would be
14.5k1919 gold badges8383 silver badges149149 bronze badges
Peter HullPeter Hull
3,21222 gold badges2626 silver badges3232 bronze badges
15 Answers
For PowerShell versions less than 3.0:
The
FileInfo object returned by Get-ChildItem has a 'base' property, PSIsContainer . You want to select only those items.
If you want the raw string names of the directories, you can do
For PowerShell 3.0 and greater:
xcudxcud
12.7k33 gold badges2929 silver badges2828 bronze badges
Marc
5,94388 gold badges3434 silver badges4848 bronze badges
iraSenthiliraSenthil
8,14366 gold badges3131 silver badges4343 bronze badges
Use
If you prefer aliases, use
or
To recurse subdirectories as well, add
-r option.
Tested on PowerShell 4.0, PowerShell 5.0 (Windows 10) and PowerShell Core 6.0 (Windows 10, Mac and Linux).
Sachin JosephSachin Joseph
10.9k33 gold badges2727 silver badges5050 bronze badges
A cleaner approach:
I wonder if PowerShell 3.0 has a switch that only returns directories; it seems like a logical thing to add.
Peter Mortensen
14.2k1919 gold badges8888 silver badges115115 bronze badges
Carlos NunezCarlos Nunez
1,59111 gold badge1515 silver badges1717 bronze badges
Peter Mortensen
14.2k1919 gold badges8888 silver badges115115 bronze badges
Marek DzikiewiczMarek Dzikiewicz
2,57911 gold badge1717 silver badges2020 bronze badges
From PowerShell v2 and newer (k represents the folder you are beginning your search at):
If you just want folder names only, and nothing else, use this:
If you are looking for a specific folder, you could use the following. In this case, I am looking for a folder called Peter Mortensen
myFolder :
14.2k1919 gold badges8888 silver badges115115 bronze badges
BillBill
31911 gold badge44 silver badges1111 bronze badges
Use:
This will give you an output of the root structure with the folder name for directories only.
Peter Mortensen
14.2k1919 gold badges8888 silver badges115115 bronze badges
WesleyWesley
jyggorathjyggorath
Use:
Which does the following
Marty GomezMarty Gomez
A bit more readable and simple approach could be achieved with the script below:
Hope this helps!
ZorayrZorayr
15.4k22 gold badges9595 silver badges8686 bronze badges
You'll want to use Get-ChildItem to recursively get all folders and files first. And then pipe that output into a Where-Object clause which only take the files.
sonjzsonjz
3,61433 gold badges3232 silver badges5454 bronze badges
Powershell Command Change Directory
The accepted answer mentions
to get a 'raw string'.But in fact objects of type
Selected.System.IO.DirectoryInfo will be returned. For raw strings the following can be used:
The difference matters if the value is concatenated to a string:
5,10722 gold badges2121 silver badges2121 bronze badges
My solution is based on the TechNet article Fun Things You Can Do With the Get-ChildItem Cmdlet.
Powershell Change Directory To Desktop
I used it in my script, and it works well.
Peter Mortensen
14.2k1919 gold badges8888 silver badges115115 bronze badges
SarrusSarrus
Peter Mortensen
14.2k1919 gold badges8888 silver badges115115 bronze badges
HansliHansli
To answer the original question specifically (using IO.FileAttributes):
Payday 2 steam community group.
Get-ChildItem c:mypath -Recurse | Where-Object {$_.Attributes -and [IO.FileAttributes]::Directory}
I do prefer Marek's solution though (
Where-Object { $_ -is [System.IO.DirectoryInfo] } ).
Nico57Nico57
Not the answer you're looking for? Browse other questions tagged powershellpowershell-v2.0 or ask your own question.
I would like to search for a folder in a specific directory and subdirectorys.
I tried googling it, but didn't really find any usefull examples.
madsmads
1 Answer
I believe there's no dedicated cmdlet for searching files.
Edit in response to @Notorious comment:Since Powershell 3.0 this is much easier, since switches
-Direcotry and -File were added to Get-ChildItem . So if you want it short you've got:
With command alias and tab-completion for switches it's a snap. I just missed that the first time.
Adam LuniewskiAdam Luniewski
7,90744 gold badges3737 silver badges5656 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Not the answer you're looking for? Browse other questions tagged powershell or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |