locked
problem with $_. RRS feed

  • Question

  • Hi,

    I have just started with powershell and programming. I am experimenting with Functions a bit. I would like to get al the files that have not been used or written to for a while.

    I have writen the script below:

    #functions
    #Function Variables
    Function Get-Variabelen{
    $script:Path =  Read-Host "Welk path wil je zoeken"
    $script:Days = Read-Host "Hoeveel Dagen"
    }

    #Function: DirectoryorFiles
    Function Get-Directoryorfiles{
    $dirorfilesvar = Read-Host "Wat wil je zien. Voor directory's [1] voor Files [2]"
    if ($dirorfilesvar -eq 1) {$script:dirorfile = "-Direcory"}
        Elseif ($dirorfilesvar -eq 2) {$script:dirorfile = "-File"}
        Else {write-host "Je hebt geen juist getal gekozen" exit}
    }

    #Functions Get-Result
    function Get-Result{
    Get-ChildItem -Path $Path -Recurse $dirorfile | Where-Object {($_.LastWriteTime -lt (Get-Date).AddMonths(-$Days))} | select FullName, LastWriteTime
    }

    Get-Variabelen
    Get-Directoryorfiles
    Get-Result

    The most part of the script works fine. But this part doesnt work:

    Get-ChildItem -Path $Path -Recurse $dirorfile | Where-Object {($_.LastWriteTime -lt (Get-Date).AddMonths(-$Days))} | select FullName, LastWriteTime

    When i execute the scipt by hand it works. But when i execute is in a scipt or function it writes this down.

    Get-ChildItem -Path $Path -Recurse $dirorfile | Where-Object {(.LastWriteTime -lt (Get-Date).AddMonths(-$Days))} | select FullName, LastWriteTime

    It always misses the $_. part after the where-object. I am doing something wrong. 

    Is there someone who knows what?

    Thanks in advance.

    Friday, January 18, 2019 7:44 AM

All replies

  • Please edit your post and format the code as code. That will avoid unwanted line breaks in your code.

    Your code is a little convoluted and overcomplicated for such a little task. But you could start with removing the parenthesis inside the curly braces of your not working Where-Object block.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Friday, January 18, 2019 8:18 AM
    Friday, January 18, 2019 8:17 AM
  • What IDE are you using to write program?

    Can you provide the error message you are getting?

    Friday, January 18, 2019 9:23 AM
  • Variables defined inside a function do not exist after the function exits.

    Please learn how to format code correctly and learn how to format you code correctly. PowerShell Style Guidelines

    Example:

    function Get-Result {
        param (
            $Path = $PWD,
            $DirOrFile = '*.*',
            $Months = 1
        )
        
        Get-ChildItem -Path $Path -Recurse -Include $DirOrFile |
            Where-Object {$_.LastWriteTime -lt (Get-Date).AddMonths(-$Months)} |
            Select-Object FullName, LastWriteTime
    }
    1. Microsoft Virtual Academy - Getting Started with Microsoft PowerShell
    2. PowerShell Documentation
    3. PowerShell Style Guidelines


    \_(ツ)_/



    Friday, January 18, 2019 1:08 PM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Lee

    Just do it.

    Monday, January 21, 2019 9:17 AM