Author Posts

November 3, 2015 at 10:39 pm

Please kindly help I am totally new to powershell and am needing help deleting old inventory from csv and related folders.
I have details as follows in the csv file:
Inv code, Inventory name, Date Entered

I have to run a function to look up the cvs file in a directory and any inventory that is older than 2 yrs, to go delete the entry from the csv file and in the same directory to delete the relevant inventory code folder.

Let's say I have:
Inv code / Inventory Name / Date Entered
MSPN001 / Metal Spoon / 2/15/2006

The same inventory has a folder in the directory that contains details of the inventory such as first batch, quantity, returns etc....
Folder name is according to the Inv Code in the cvs file, so for the above would be:
MSPN001

I need the function to look up only the ones older than 2 years and delete.

Sorry I am still in the learning phase and due to a colleague on long term leave I had to learn and do this.

Thanks a bunch!

November 4, 2015 at 6:43 am

This might get you going in the right direction. Maybe some of the other guys can chime in.

$data = @()
$InvDate = @()
$Inventory = Import-Csv -Path "C:\somepath\inventory.csv" 
$Date = Get-Date -Format MM/dd/yyyy
Foreach($Item in $Inventory){
    $InvDate =  New-TimeSpan -Start $item.'Date Entered' -End $Date
    if($InvDate.Days -ge 730){
        if(Test-Path "C:\SomePath\$($Item.'Inv code')"){
            Del "C:\SomePath\$($Item.'Inv code')"
        }
    }
    Else{
        $data += New-Object psobject -Property ([Ordered]@{'Inv code' =  $item.'Inv code'; 'Inventory Name' = $item.'Inventory Name '; 'Date Entered' = $item.'Date Entered'})
    }
$data | Export-Csv -Path "C:\somepath\inventory.csv" 
}

November 4, 2015 at 10:43 am

here is it as a function

you would use it by dot sourcing it or run it then enter the following in the shell.

Delete-Inventory -FilePath c:\somepath\inventory.csv

This will delete folders from c:\somepath\MSPN001 if the date in 'Date Entered' field exceeds 2 years then it will update the csv file. if there are multiple items in the csv file it will cycle through all items and delete the folders for the items that exceed the 2 year date and then update the csv file. Also keep in mind the code only is rebuilding the csv with the 3 columns you posted. If your csv has more columns then you listed lit me know I will rewrite it for you.

this script was tested on my machine with dummy data. make sure you test before applying to production environment.

Function Delete-Inventory {
Param(
    [Parameter(Mandatory=$True)]
    [string]$FilePath
)
$data = @()
$InvDate = @()
$path = Get-ChildItem -Path $FilePath
$Inventory = Import-Csv -Path $FilePath 
$Date = Get-Date -Format MM/dd/yyyy
Foreach($Item in $Inventory){
    $InvDate =  New-TimeSpan -Start $item.'Date Entered' -End $Date
    if($InvDate.Days -ge 730){
        if(Test-Path ($($path.DirectoryName) + "\" + $($Item.'Inv code'))){
            Del ($($path.DirectoryName) + "\" + $($Item.'Inv code'))
        }
    }
    Else{
        $data += New-Object psobject -Property ([Ordered]@{'Inv code' =  $item.'Inv code'; 'Inventory Name' = $item.'Inventory Name '; 'Date Entered' = $item.'Date Entered'})
    }
$data | Export-Csv -Path $FilePath
}
}

November 12, 2015 at 11:39 pm

sorry i am still working on it but could i get a hint on this line of my script which is showing 'Path' because it is null

if(test-path ($($path.directoryname) "\" $(item.'invcode')))

thanks

November 13, 2015 at 4:21 am

when you run the function the first time the value is null

next run the function like this
Delete-Inventory -FilePath c:\somepath\inventory.csv
with c:\somepath\inventory.csv as the path to your csv file
make sure you do this to a test file to make sure it is what you wanted.

can some one else chime in maybe i'm not the best at explaining functions 😀