Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell getting full path information

Tags:

powershell

I have a directory called Videos. Inside this directory, are a bunch of sub directories of various cameras. I have a script that will check each of the various cameras, and delete recordings older than a certain date.

I am having a bit of trouble getting the full directory information for the cameras. I am using the following to get it:

#Get all of the paths for each camera
$paths = Get-ChildItem -Path "C:\Videos\" | Select-Object FullName

And then I loop through each path in $paths and delete whatever I need to:

foreach ($pa in $paths) {
    # Delete files older than the $limit.
    $file = Get-ChildItem -Path $pa -Recurse -Force | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit } 
    $file | Remove-Item -Recurse -Force
    $file | Select -Expand FullName | Out-File $logFile -append
}

When I run the script, I am getting errors such as:

@{FullName=C:\Videos\PC1-CAM1}
Get-ChildItem : Cannot find drive. A drive with the name '@{FullName=C' does not exist.
At C:\scripts\BodyCamDelete.ps1:34 char:13
+     $file = Get-ChildItem -Path $pa -Recurse -Force | Where-Object { $_.PSIsCont ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (@{FullName=C:String) [Get-ChildItem], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Is there a way to strip that @{FullName= off of the Path? I think that may be what the issue is.

like image 713
Talen Kylon Avatar asked Apr 17 '26 22:04

Talen Kylon


1 Answers

In your case $pa is an object with a FullName property. The way you would access that would be this.

$file = Get-ChildItem -Path $pa.FullName -Recurse -Force | Where-Object { $_.PSIsContainer -and $_.CreationTime -lt $limit } 

However it would just be simpler to change only this line and leave

$paths = Get-ChildItem -Path "C:\Videos\" | Select-Object -ExpandProperty FullName

-ExpandProperty will just return the string instead of the object that Select-Object was returning.

like image 130
Matt Avatar answered Apr 20 '26 20:04

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!