Any ideas how to use the command 'dir' to more than 1 folder in one command?
dir c:\A | c:\test\B
is not working. Im not good with pipes...
Use the Get-ChildItem cmdlet instead, or use DIR and separate your sources by commas, not spaces. DIR doesn't work the same in PowerShell as it does in a DOS window.
http://technet.microsoft.com/en-us/library/ee176841.aspx
dir c:\A c:\test\B should work. No pipe necessary.
Related
I'm trying to translate a Linux command to be easily usable for Windows user for a project, but I am not having any luck finding comparable commands in Powershell.
I have two paths with some SQL and CSV files. What I need is this command:
cat ./* ../path/* > new_file.sql
This takes all content from all files in path1 and then all content from all files in path2 and writes it to a file.
I assumed I could do something similar in Powershell, but apparently the behaviour is wildly different.
What I have tried are:
cat ./*, ../path/* > new_file.sql
Get-Content ./*, ../path2/* | Out-File new_file.sql
They both do the same which seems to... I'm not sure, take the entirety of path2/* for every file in path1? The output quickly balloons to tens of megabytes. The combined content of both directories is a perhaps 40 kilobytes.
Anyone know? I cannot find a proper answer to this. Thanks!
EDIT: I think I figured out what the problem is. I guess I should've just used the actual paths for the example. First path is ./* and it seems like it keeps looping over the Out-File it makes itself. I have updated the title and examples to reflect this.
Enumerate the files as a separate step before concatenating their contents (this way Get-Content won't accidentally discover the new file halfway through):
$files = Get-ChildItem ./,../path2/ -File
$files |Get-Content |Out-File newfile.txt
You can combine these statements in a single pipeline if you wish:
(Get-ChildItem ./,../path2/ -File) |Get-Content |Out-File newfile.txt
My PowerShell prompt's currently pointed to my C drive (PS C:\>). How do I change directory to a folder on my Q (PS Q:\>) drive?
The folder name on my Q drive is "My Test Folder".
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
PS C:\> Set-Location -Path Q:\MyDir
PS Q:\MyDir>
By default in PowerShell, CD and CHDIR are alias for Set-Location.
(Asad reminded me in the comments that if the path contains spaces, it must be enclosed in quotes.)
To go directly to that folder, you can use the Set-Location cmdlet or cd alias:
Set-Location "Q:\My Test Folder"
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 "".
You can simply type Q: and that should solve your problem.
Set-Location -Path 'Q:\MyDir'
In PowerShell cd = Set-Location
You can also use the sl command to be able to change directories. It is Set-Location but it is much shorter.
Example:
# Too verbose
Set-Location -Path C:\
# Just the right amount of characters to type
sl C:\
If your Folder inside a Drive contains spaces In Power Shell you can Simply Type the command then drive name and folder name within Single Quotes(''):
Set-Location -Path 'E:\FOLDER NAME'
The Screenshot is attached here
On Powershell use Set-Location instead of cd.
Put path in quotes. Single quotes works for me.
Set-Location 'C:\Program Files\MongoDB\Server\6.0'
I have a bunch of folders with similar prefixes in the name, and I'd like to make symbolick links to these folders so that I can remove the prefix while respecting the original folder naming convention. Here are some example folder names:
2013.Folder1
2013.Folder2
2014.Folder1
in the folder:
C:\Users\madeupname\Documents
In linux, I'd just do
ln -s /home/madeupname/Documents/201* /home/madeupname/Documents/links/
(this code may not exactly right as I don't have a linux box handy right now)
In Windows PowerShell, I could do it manually for these 3 files:
cmd /c mklink C:\Users\madeupname\Documents\links\2013.Folder1 C:\Users\madeupname\Documents\2013.Folder1
but that is no good because the real directory has a lot of files!
if I understood correctly this can work for you:
$path = "C:\Users\madeupname\Documents"
dir $path -Directory |
% { cmd /c mklink C:\Users\madeupname\Documents\links\$_.name $_.fullname /d}
I found a GUI to do this, but that's cheating so I won't mark this as the answer:
http://schinagl.priv.at/nt/hardlinkshellext/hardlinkshellext.html
I was copying a huge number of png and txt files using Copy-Item cmdlet, and sadly I discovered that a funny programmer decided to use "CON" as file name to recap connection information.
Given that "con" is a reserved word and Copy-Item returns:
Copy-Item : Cannot process path 'xxx\con.txt' because the target represents a reserved device name.
and given that this name can't be changed and it's used in every folder I need to copy,
Is there a way to copy all these "con.cfg" and "con.txt" files using Powershell?
I googled but I found only advice like "Don't use con!" or "Don't use Powershell to copy these files".
I haven't been able to find a solution for PowerShell yet, but you should be able to rename the files via command prompt using something like this:
ren \\.\<absolute path> <new name>
So for example:
ren \\.\C:\stuff\con.cfg stuff.cfg
You could invoke the command prompt through PowerShell, of course:
cmd /c "ren \\.\C:\stuff\con.cfg stuff.cfg"
And obviously you could use PowerShell variables in there if you wanted
$dir = "C:\stuff"
cmd /c "ren \\.\$dir\con.cfg stuff.cfg"
You could try referring to them using a wildcard: *on ?
Example:
ls | ? {$_.Name -match "*on.cfg"} | del
Regex example:
ls | ? {$_.Name -match "^\won\.cfg"} | del
I simply want to list all of the directories under my current working directory, using PowerShell. This is easy from a Bash shell:
ls -d */
or cmd.exe in Windows:
dir /a:d
Using PowerShell however I cannot seem to be able to do it with a single command. Instead the only the I've found that works is:
ls | ? {$_Mode -like "d*"}
That seems way too wordy and involved, and I suspect that I don't need a separate Where clause there. The help for Get-ChildItem doesn't make it clear how to filter on Mode though. Can anyone enlighten me?
This works too:
ls | ?{$_.PsIsContainer}
There is no doubt that it is a little more wordy than bash or cmd.exe. You could certainly put a function and an alias in your profile if you wanted to reduce the verbosity. I'll see if I can find a way to use -filter too.
On further investigation, I don't believe there is a more terse way to do this short of creating your own function and alias. You could put this in your profile:
function Get-ChildContainer
{
param(
$root = "."
)
Get-ChildItem -path $root | Where-Object{$_.PsIsContainer}
}
New-Alias -Name gcc -value Get-ChildContainer -force
Then to ls the directories in a folder:
gcc C:\
This solution would be a little limited since it would not handle any fanciness like -Include, -Exclude, -Filter, -Recurse, etc. but you could easily add that to the function.
Actually, this is a rather naive solution, but hopefully it will head you in the right direction if you decide to pursue it. To be honest with you though I wouldn't bother. The extra verbosity in this one case is more than overcome by the overall greater flexibility of powershell in general in my personal opinion.
Try:
ls | ? {$_.PsIsContainer}
dir -Exclude *.*
I find this easier to remember than
dir | ? {$_.PsIsContainer}
Plus, it is faster to type, as you can do -ex instead of -exclude or use tab to expand it.
You can now use Get-ChildItem -Directory or ls -dir for short. This has existed at least since PowerShell 3.0 according to Microsoft's documentation.
You can check old post on PowerShell team blog: http://blogs.msdn.com/powershell/archive/2009/03/13/dir-a-d.aspx
I came to this thread because I'm trying to figure out how "FOR /D" works. Well actually how to use the batch-command escape(%) with the /D option.
I read the above items with hope, to be honest they're all a lot more complex than the FOR command option -- If it worked of course.
Using additional forms of for
If command extensions are enabled (that is, the default), the following additional forms of for are supported:
Directories only
If set contains wildcards (* and ?), the specified command executes for each directory (instead of a set of files in a specified directory) that matches set. The syntax is:
for /D {%% | %}variable in (set) do command [CommandLineOptions]
Well, soon we will have to a make a class to simplify things and be typing commands like
dir -options {122b312aa3132-1313-131112f8111111} just so we can do the same as
dir/ad/od