I used to use paths like this ls {path1,path2}/* in Linux but in Windows PowerShell, I got the following error
At line:1 char:10
+ ls {path1,path2}/*
+ ~
Missing argument in parameter list.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingArgument
I tried dir {path1,path2}/* instead of ls {path1,path2}/* but the same error thrown!!
You should use the proper cmdlet/alias for the environment you are working in (consider on linux PowerShell Core ls refers to the system ls not the PoSh gci)
PowerShell allows wildcards *?(and ranges[0-2]) on multple levels, so this should do:
Get-ChildItem -Path some\verbose[12]\dir\* -Include *.ext1,*.ext2
In case the part of the path differs completely you'll have to supply an array:
Get-ChildItem -Path some\this\dir\*,some\that\dir\* -Include *.ext1,*.ext2
I think what you want is this:
ls path1,path2 -recurse
Path1 and Path2 will either need to be variables or quoted Strings.
Here is an example
PS C:\> ls "c:\temp\test1","c:\temp\test2" -recurse
Directory: C:\temp\test1
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2/14/2019 10:29 AM test1subdir
Directory: C:\temp\test1\test1subdir
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/14/2019 10:29 AM 13 test1file.txt
Directory: C:\temp\test2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/14/2019 10:30 AM 6 test2file.log
I know this may be crazy, but you can create a function that will nearly replicate what you are trying to do:
function new-ls {
param([Parameter(Position=0)]
[string]$Path,
[Parameter(Position=1)]
[string[]]$Includes)
$StrMatch = ($Path | sls -pattern "^(.*?){(.*?),(.*?)}(.*?)$" -allmatches).matches.groups.value
$NewPaths = #(($StrMatch[1] + $StrMatch[2] + $StrMatch[4]), ($StrMatch[1] + $StrMatch[3] + $StrMatch[4]))
ls $NewPaths -include $Includes -recurse
}
# Running the code
PS C:\temp> new-ls "c:\temp\{test1,test2}\*"
Directory: C:\temp\test1\test1subdir
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/14/2019 10:29 AM 13 test1file.txt
Directory: C:\temp\test2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/14/2019 10:30 AM 6 test2file.log
PS C:\temp> new-ls "c:\temp\{test1,test2}\*" "*.txt"
Directory: C:\temp\test1\test1subdir
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/14/2019 10:29 AM 13 test1file.txt
PS C:\temp> new-ls "c:\temp\{test1,test2}\*" "*.txt","*.log"
Directory: C:\temp\test1\test1subdir
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/14/2019 10:29 AM 13 test1file.txt
Directory: C:\temp\test2
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/14/2019 10:30 AM 6 test2file.log
Related
I want to output the last entry of Get-Hotfix in PowerShell. How does it work?
Example:
Source Description HotFixID InstalledBy InstalledOn
------ ----------- -------- ----------- -----------
DESKTOP-FU... Update KB5010472 NT-AUTORITÄT\SYSTEM 15.03.2022 00:00:00
DESKTOP-FU... Update KB5003791 06.10.2021 00:00:00
DESKTOP-FU... Security Update KB5011487 NT-AUTORITÄT\SYSTEM 14.03.2022 00:00:00
DESKTOP-FU... Update KB5007273 NT-AUTORITÄT\SYSTEM 02.01.2022 00:00:00
DESKTOP-FU... Security Update KB5011352 NT-AUTORITÄT\SYSTEM 09.02.2022 00:00:00
DESKTOP-FU... Security Update KB5005699 06.10.2021 00:00:00
I want to output the KB5005699 update to the console.
You can use Select-Object with the -Last 1 parameter to get the last object and -ExpandProperty HotFixID to get the Value of the HotFixID property:
Get-HotFix | Select-Object -Last 1 -ExpandProperty HotFixID
Another alternative would be to get the last object by the -1 index and use dot notation .HotFixID to get the Value:
(Get-HotFix)[-1].HotFixID
I have on my system some SD card or removable media drive which by default takes the D drive letter.
I have this:
PS C:\Windows\System32\WindowsPowerShell\v1.0> get-volume | where DriveType -Match "Removable"
DriveLetter FileSystemLabel FileSystem DriveType HealthStatus SizeRemaining Size
----------- --------------- ---------- --------- ------------ ------------- ----
E Removable Healthy 0 B 0 B
And I want to change it to G like this:
PS C:\Windows\System32\WindowsPowerShell\v1.0> get-volume | where DriveType -Match "Removable" |Set-Partition -NewDriveLetter G
But I get:
Set-Partition : One or more parameter values passed to the method were invalid.
At line:1 char:50
+ ... | where DriveType -Match "Removable" |Set-Partition -NewDriveLetter G
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (PS_StorageCmdlets:ROOT/Microsoft/..._StorageCmdlets) [Set-Partition],
CimException
+ FullyQualifiedErrorId : MI RESULT 4,Set-Partition
How can I change this? Or what is the issue? I tried with the get-disk to pass but doesn't work either.
I can change in the computer management gui but I need commandline.
You should be able to do it with something like this.
$DriveLetter = (Get-Volume | Where-Object {$_.DriveType -eq "Removable"}).DriveLetter
Get-Partition -DriveLetter $DriveLetter | Set-Partition -NewDriveLetter G
Removable drives (at least without media) does not work with Set-Partition
Worked around this by scripting diskpart
"SELECT VOLUME D`nREMOVE LETTER=D`nASSIGN LETTER=G NOERR" | diskpart
This selects volume d, removes given letter and assigns new letter (ignoring error)
Using Powershell I am trying to put the results of three commands into a table and output them to a file, then repeat the command forever. I can't figure out exactly how to format the table correctly.
This is my script
while (1){
$ping = test-connection 8.8.8.8 -delay 1 -count 1
$wifi = #{n='Status';e={get-netadapter -physical -name Wi-Fi | select Status}}
$timestamp = #{n='TimeStamp';e={Get-Date}}
$ping | format-table __SERVER, Address, ResponseTime, $timestamp, $wifi | out-file "C:\test-connection.txt" -append
start-sleep -s 10
}
$ping gets the ping results of 8.8.8.8
$wifi gets the status of the wi-fi adapter
$timestamp gets the current time
The final output is meant to look something like this:
__SERVER Address ResponseTime TimeStamp Status
-------- ------- ------------ --------- ------
Hostname 8.8.8.8 19 18/02/2019 10:19:23 Up
Hostname 8.8.8.8 19 18/02/2019 10:19:23 Up
Hostname 8.8.8.8 19 18/02/2019 10:19:23 Up
Hostname 8.8.8.8 19 18/02/2019 10:19:23 Up
...
However, with my current setup it does this:
__SERVER Address ResponseTime TimeStamp Status
-------- ------- ------------ --------- ------
Hostname 8.8.8.8 20 18/02/2019 10:19:13 #{Status=Up}
__SERVER Address ResponseTime TimeStamp Status
-------- ------- ------------ --------- ------
Hostname 8.8.8.8 19 18/02/2019 10:19:23 #{Status=Up}
__SERVER Address ResponseTime TimeStamp Status
-------- ------- ------------ --------- ------
Hostname 8.8.8.8 20 18/02/2019 10:19:33 #{Status=Up}
Any assistance is appreciated.
You would have to hide the table headers on subsequent writes, and also trim the output.
Try this:
while (1) {
$ping = test-connection 8.8.8.8 -delay 1 -count 1
$wifi = #{n='Status';e={get-netadapter -physical -name Wi-Fi | select -expand Status}}
$timestamp = #{n='TimeStamp';e={Get-Date}}
$path = "C:\test-connection.txt"
$ping | ft __SERVER, Address, ResponseTime, $timestamp, $wifi -Hide:(Test-Path $path) | out-string | % {$_.trim()} | out-file $path -append
start-sleep -s 10
}
Using Format-Table for file output is not a good idea though, it's meant for display in the console. Consider using CSV or a custom format.
i am using following powershell script for getting folder with latest time stamp
$drive = get-psdrive |select root |Select-String -InputObject {$_.Root} -Pattern ':'
Write-Host $drive
foreach ($a in $drive)
{
Get-ChildItem -Path $a -Filter "*sysout" -Recurse -Force -ErrorAction SilentlyContinue|select -last 1
}
}
it gives following output
A:\ C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\ K:\ Z:\
Directory: E:\utility
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2/21/2018 2:06 AM sysout
Directory: F:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2/21/2018 3:15 AM sysout
i need latest timestamp folder but using -last 1 doesnt give desired results.
Last doesn't really have any meaning without order.
Get-ChildItem -Path $a -Filter "*sysout" -Recurse -Force -ErrorAction SilentlyContinue |
Sort-Object LastWriteTime -Descending |
Select-Object -Last 1
I wrote a powershell script that exports a bunch of files from a Dynamics NAV instance. It calls a perl script that I also wrote that then splits all of the files into individual objects and sticks them in subdirectories under a dir I create in perl. Then the powershell script attempts to copy the files to a different dir, and fails.
Powershell generates the dir name:
$datestamp = get-date -f MM-dd-yyyy_HH_mm_ss
$dumpdir = "\temp\nav_export\" + $env:username + "\" + $servicetier + "~" + $database + "~" + $datestamp;
Then powershell does a bunch of stuff that works fine, and calls the perl script ($servicetier and $database are defined earlier in the script):
& c:\navgit\split-objects.pl $servicetier $database $datestamp
perl proceeds to create the directory and split the files correctly:
use strict;
use warnings;
use File::Path qw(make_path remove_tree);
my $username = getlogin || getpwuid($<);
my $servicetier = $ARGV[0];
my $database = $ARGV[1];
my $datestamp = $ARGV[2];
undef #ARGV;
my $work_dir = "/temp/nav_export";
my $objects_dir = "$work_dir/$username/objects";
my $export_dir = "$work_dir/$username/$servicetier~$database~$datestamp";
print "Objects from $servicetier~$database being exported to $export_dir\n";
make_path("$export_dir/Page", "$export_dir/Codeunit", "$export_dir/MenuSuite", "$export_dir/Query", "$export_dir/Report", "$export_dir/Table", "$export_dir/XMLport");
chdir $objects_dir or die "Could not change to $objects_dir: $!";
<does all of the filehandling and parsing>
Control returns to the powershell script, which tries to finish with:
Copy-Item -Path $dumpdir -Destination $cwd -Force -Recurse
But that throws the error:
Copy-Item : Cannot find path 'C:\temp\nav_export\danielj\cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_19_26_50' because it does not exist.
At C:\navgit\nav-export.ps1:175 char:1
+ Copy-Item -Path $dumpdir -Destination $cwd -Force -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\temp\nav_exp...0-2015_19_26_50:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
The directory I'm trying to copy from does exist. But powershell doesn't see it! I added some code to list the contents of the parent dir:
Copy-Item -Path $dumpdir -Destination $cwd -Force -Recurse
Write-Host "Copy-Item -Path $dumpdir -Destination $cwd -Force -Recurse"
$test = "C:\temp\nav_export\$env:username"
Get-ChildItem $test -Force
Copy-Item -Path \temp\nav_export\danielj\cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_19_26_50 -Destination C:\Users\danielj\erp\ -Force -Recurse
Directory: C:\temp\nav_export\danielj
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 1/20/2015 6:32 PM cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_18_32_33
d---- 1/20/2015 7:08 PM cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_19_08_49
d---- 1/19/2015 1:07 PM cen-dev-erp-st1~JustFoodERP-PROTO~20150119-130747
d---- 1/20/2015 7:26 PM logs
d---- 1/20/2015 7:26 PM objects
-a--- 1/20/2015 7:26 PM 309 objects.bat
-a--- 1/20/2015 1:41 PM 436 soap_envelope.txt
If I do a directory listing from outside the script, there it is:
PS C:\Users\danielj\erp> $test = "C:\temp\nav_export\$env:username"
Get-ChildItem $test -Force
Directory: C:\temp\nav_export\danielj
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 1/20/2015 6:32 PM cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_18_32_33
d---- 1/20/2015 7:08 PM cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_19_08_49
d---- 1/20/2015 7:26 PM cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_19_26_50
d---- 1/19/2015 1:07 PM cen-dev-erp-st1~JustFoodERP-PROTO~20150119-130747
d---- 1/20/2015 7:26 PM logs
d---- 1/20/2015 7:26 PM objects
-a--- 1/20/2015 7:26 PM 309 objects.bat
-a--- 1/20/2015 1:41 PM 436 soap_envelope.txt
I've tried calling an external script from the main powershell script after perl is finished, and the results are the same.
Why would powershell not see the directory or files that were created by the perl script? And more importantly, how can I get it to do so?
Are you sure that everything proced in the exact order you assume it does ?
try to start your perl script like this :
& c:\navgit\split-objects.pl $servicetier $database $datestamp | Out-Null
It makes sure that your Perl part is terminated when the PowerShell execute the rest of your script.
I ended up just creating the directory in powershell in the first place. No reason it should have to have been created in the perl script.
It'd still be nice to know why it didn't work the way I'd have expected though.