Error reading file share paths as arguments - powershell

I am receiving the following error when I execute the below script.
Test-Path : A positional parameter cannot be found that accepts
argument 'input.dat'.
.\FL.ps1 \\flamingdev\analytics\source\INBOUND \\flamingdev\analytics\source\OUTBOUND
[CmdletBinding()]
param (
[string] $SrcFolder,
[string] $FileListPath
)
$SrcFolder
$FileListPath
IF (Test-Path "$FileListPath"\input.dat) {
Remove-Item "$FileListPath"\input.dat
}
Get-ChildItem -File -Path "$SrcFolder"\Extract* | Select-Object - ExpandProperty Name | Add-Content -Path "$FileListPath"\input.dat

You meed to take "all path" in double quotes. Like this:
IF (Test-Path "$FileListPath\input.dat") {
Remove-Item "$FileListPath\input.dat"
}
Get-ChildItem -File -Path "$SrcFolder\Extract*" | Select-Object -ExpandProperty Name | Add-Content -Path "$FileListPath\input.dat"

Thank you, issue was resolved after tweaking the code little bit. Get-ChildItem -File -Path "$SrcFolder","*.csv"

Related

Check if files exists in PowerShell

I'm trying to copy some files from one diretory to another, check if exists and replace name if yes. And copy all files.
But it gives me the above message:
cmdlet Copy-Item at command pipeline position 1
Supply values for the following parameters:
Path[0]:
PS C:\Scripts\MetadataExport>
What i'm doing wrong?
My code:
Get-ChildItem -Path "C:\Scripts\MetadataExport\*\*" -Directory | ForEach-Object {
$_.FullName | ForEach-Object {Get-ChildItem $_ *.opex | ForEach-Object{If([System.IO.File]::Exists("C:\Scripts\MetadataExport\$Bundles")){While($true){$i=0
$i++
$_.Name= $_+$i}
}Else{Copy-Item -Destination "C:\Scripts\MetadataExport\$Bundles" }}
Get-ChildItem $_ -Recurse -Include *.xip | ForEach-Object{If([System.IO.File]::Exists("C:\Scripts\MetadataExport\$Bundles")){While($true){$i=0
$i++
$_.Name= $_+$i}
}Else{Copy-Item -Destination "C:\Scripts\MetadataExport\$Resources"}}
}
}
Thanks for any help on this
The syntax is Copy-Item -Path "yourpathhere" -Destination "yourdestinationhere"
You've not specified path.

Powershell delete all folders except one

I tried
Get-ChildItem -Path 'C:\temp' -Recurse Select Name | Where {$_ -notlike 'C:\temp\one*'} | sort length -Descending | Remove-Item -force
but it doesn't work
Get-ChildItem : A positional parameter cannot be found that accepts argument 'Name'
What's wrong
You were missing a |
Get-ChildItem -Path 'C:\temp' -Recurse | Select -ExpandProperty FullName | Where {$_ -notlike 'C:\temp\one*'} | Remove-Item -force
Try this with -Exclude (And why sort when deleting files?)
Get-ChildItem -Path 'C:\temp' -Recurse -Exclude 'C:\temp\one*' | Remove-Item -force
Use the function below:
Function Delete-Except
{
$path = ""
$exceptions = #(
#Enter files/folders to omit#
)
try:
Get-ChildItem $source -Exclude $exceptions| Remove-Item $_ -Force -Recurse
catch:
Write-Host "Delete operation failed." - Foregroundcolor Red
Pause
}

Powershell v5 Get-ChildItem -File not working?

Get-ChildItem -Path $dir -Recurse -File | ForEach-Object `
{
Write-Host $_.fullname
}
Get-ChildItem : A parameter cannot be found that matches
parameter name 'File'.
Version
-------
5.1.17763.771
I have many other ps scripts running that use this and it works fine, but for some reason it's not working in this new script I wrote. What's going on?
Thank you everyone for helping.
The value of $dir was
" S:\folder\folder\"
Removing the space before the drive letter resolved the issue.
In addition to the OP's particular instance, there are other cases where this error can occur. Get-ChildItem can be used on other providers besides the file provider (e.g., the registry), and the -File switch is not valid in those cases.
Example:
$dir = 'C:\Temp'
Get-ChildItem -Path $dir -File # Works just fine
$dir = 'HKCU:\Software'
Get-ChildItem -Path $dir # Works without -File switch
Get-ChildItem -Path $dir -File # Throws "parameter cannot be found" error
That wasn't the source of the problem for me.
All I did was surround the variable with ( ) and that resolve it.
In my case I wanted the full filename of the 'found' file
$curdir = "C:\Program Files\"
(Get-Childitem -Path ($curdir) -File -filter "node.exe" -recurse -Force -ErrorAction SilentlyContinue).Directory.FullName
In your case I'm sure just using this would work for you:
Get-ChildItem -Path ($dir) -Recurse -File | ForEach-Object `
{
Write-Host $_.fullname
}

How to compress multiple files into one zip with PowerShell?

I want to compress multiple files into one zip.
I am stuck with this at the moment:
Get-ChildItem -path C:\logs -Recurse | Where {$_.Extension -eq ".csv" -and $_.LastWriteTime -lt (Get-Date).AddDays(-7)} | write-zip -level 9 -append ($_.LastWriteTime).zip | move-item -Force -Destination {
$dir = "C:\backup\archive"
$null = mkdir $dir -Force
"$dir\"
}
I get this exception
Write-Zip : Cannot bind argument to parameter 'Path' because it is null.
This part is the problem:
write-zip -level 9 -append ($_.LastWriteTime).zip
I have never used powershell before but i have to provide a script, I can't provide a c# solution.
The problem is that Get-ChildItem returns instances of the System.IO.FileInfo class, which doesn't have a property named Path. Therefore the value cannot be automatically mapped to the Path parameter of the Write-Zip cmdlet through piping.
You'll have to use the ForEach-Object cmdlet to zip the files using the System.IO.FileInfo.FullName property, which contains the full path:
Get-ChildItem -Path C:\Logs | Where-Object { $_.Extension -eq ".txt" } | ForEach-Object { Write-Zip -Path $_.FullName -OutputPath "$_.zip" }
Here's a shorter version of the command using aliases and positional parameters:
dir C:\Logs | where { $_.Extension -eq ".txt" } | foreach { Write-Zip $_.FullName "$_.zip" }
Related resources:
Cool Pipeline Tricks, Redux (TechNet Magazine)

How to write a PowerShell function to get directories?

Using PowerShell I can get the directories with the following command:
Get-ChildItem -Path $path -Include "obj" -Recurse | `
Where-Object { $_.PSIsContainer }
I would prefer to write a function so the command is more readable. For example:
Get-Directories -Path "Projects" -Include "obj" -Recurse
And the following function does exactly that except for handling -Recurse elegantly:
Function Get-Directories([string] $path, [string] $include, [boolean] $recurse)
{
if ($recurse)
{
Get-ChildItem -Path $path -Include $include -Recurse | `
Where-Object { $_.PSIsContainer }
}
else
{
Get-ChildItem -Path $path -Include $include | `
Where-Object { $_.PSIsContainer }
}
}
How can I remove the if statement from my Get-Directories function or is this a better way to do it?
Try this:
# nouns should be singular unless results are guaranteed to be plural.
# arguments have been changed to match cmdlet parameter types
Function Get-Directory([string[]]$path, [string[]]$include, [switch]$recurse)
{
Get-ChildItem -Path $path -Include $include -Recurse:$recurse | `
Where-Object { $_.PSIsContainer }
}
This works because -Recurse:$false is the same has not having -Recurse at all.
In PowerShell 3.0, it is baked in with -File -Directory switches:
dir -Directory #List only directories
dir -File #List only files
The answer Oisin gives is spot on. I just wanted to add that this is skirting close to wanting to be a proxy function. If you have the PowerShell Community Extensions 2.0 installed, you already have this proxy function. You have to enable it (it is disabled by default). Just edit the Pscx.UserPreferences.ps1 file and change this line so it is set to $true as shown below:
GetChildItem = $true # Adds ContainerOnly and LeafOnly parameters
# but doesn't handle dynamic params yet.
Note the limitation regarding dynamic parameters. Now when you import PSCX do it like so:
Import-Module Pscx -Arg [path to Pscx.UserPreferences.ps1]
Now you can do this:
Get-ChildItem . -r Bin -ContainerOnly