Windows Vista Forums

Creating new functions using new-item cmdlet

  1. #1


    dreeschkind Guest

    Creating new functions using new-item cmdlet

    Why can't the new-item cmdlet just ask the user for the "value" argument if
    it needs it to create an item?
    I think it would be cool if cmdlets like this would also work
    'interactively' without knowing all arguments/parameters in advance.


    PoSh 5 [137/] Function:\>new-item

    cmdlet new-item at command pipeline position 1
    Supply values for the following parameters:
    Path[0]: testf
    Path[1]:
    New-Item : Cannot process argument because the value of argument "value" is
    null.
    At line:1 char:8
    + new-item <<<<



    PoSh 6 [137/] Function:\>new-item -value {write-host "test"}

    cmdlet new-item at command pipeline position 1
    Supply values for the following parameters:
    Path[0]: testf
    Path[1]:

    CommandType Name
    Definition
    ----------- ----
    ----------
    Function testf
    write-host "test"

      My System SpecsSystem Spec

  2. #2


    DBMwS Guest

    RE: Creating new functions using new-item cmdlet

    "dreeschkind" wrote:

    > Why can't the new-item cmdlet just ask the user for the "value" argument if
    > it needs it to create an item?
    > I think it would be cool if cmdlets like this would also work
    > 'interactively' without knowing all arguments/parameters in advance.
    >
    >
    > PoSh 5 [137/] Function:\>new-item
    >
    > cmdlet new-item at command pipeline position 1
    > Supply values for the following parameters:
    > Path[0]: testf
    > Path[1]:
    > New-Item : Cannot process argument because the value of argument "value" is
    > null.
    > At line:1 char:8
    > + new-item <<<<


    I am not sure why you were getting that "value is null" error because when I
    just pressed "enter" at "path[1]:", New-Item asked me for "type" of New item
    to create like the following:
    [^_^]PS[53]>New-Item
    cmdlet New-Item at command pipeline position 1
    Supply values for the following parameters:
    Path[0]: testf
    Path[1]:
    Type: file
    Directory: Microsoft.PowerShell.Core\FileSystem::C:\Program
    Files\Windows PowerShell\v1.0
    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a--- 06/19/2006 오후 12:50 0 testf
    [^_^]PS[54]>


    >
    > PoSh 6 [137/] Function:\>new-item -value {write-host "test"}
    >
    > cmdlet new-item at command pipeline position 1
    > Supply values for the following parameters:
    > Path[0]: testf
    > Path[1]:
    >
    > CommandType Name
    > Definition
    > ----------- ----
    > ----------
    > Function testf
    > write-host "test"


    And the reason that "New-Item" doesn't ask for "value" is because,
    PowerShell prompts user for an input only for mandatory parameters. If you
    look at "new-item -?", "-Value" parameter's not required

    -Value <System.Object>
    The value the new item is to have.

    Parameter required? false

    So I think what you should be suggesting is, "-Value" parameter should be
    mandator...
    But I don't think that should be the case since, under file system provider,
    one should be able to create a file or a directory any values...

      My System SpecsSystem Spec

  3. #3


    dreeschkind Guest

    RE: Creating new functions using new-item cmdlet

    "DBMwS" wrote:

    > I am not sure why you were getting that "value is null" error because when I
    > just pressed "enter" at "path[1]:", New-Item asked me for "type" of New item
    > to create like the following:


    The reason seems to be that you created a FileSystem item. It works for me
    the same.

    > And the reason that "New-Item" doesn't ask for "value" is because,
    > PowerShell prompts user for an input only for mandatory parameters. If you
    > look at "new-item -?", "-Value" parameter's not required
    >
    > -Value <System.Object>
    > The value the new item is to have.
    >
    > Parameter required? false
    >
    > So I think what you should be suggesting is, "-Value" parameter should be
    > mandator...
    > But I don't think that should be the case since, under file system provider,
    > one should be able to create a file or a directory any values...


    I think it depends on the psProvider, which parameters are mandatory.
    New items in the function: psDrive obviously need the -Value as a funtion
    definition.
    Thus, the help on New-Item seems to be confusing to me.

    --
    greetings
    dreeschkind

      My System SpecsSystem Spec

  4. #4


    DBMwS Guest

    RE: Creating new functions using new-item cmdlet

    > I think it depends on the psProvider, which parameters are mandatory.
    > New items in the function: psDrive obviously need the -Value as a funtion
    > definition.

    I think you are right about new-item hehaving differently according to
    providers.
    When you write a custom provider, as far as i remember, you can specify
    whether a parameter should be a mandatory one or not.

    When i went to "function:/" drive and typed "(gcm New-Item).Definition"
    (which should display New-Item parameter definition, specific to "Function"
    provider)
    in both parametersets, "-Value" parameter is still not a mandatory parameter.
    I guess in that case, having "-Value" as a non-mandatory parameter doesn't
    seem to make sense...

      My System SpecsSystem Spec

  5. #5


    DBMwS Guest

    RE: Creating new functions using new-item cmdlet

    > When you write a custom provider, as far as i remember,
    > you can specify whether a parameter should be a mandatory one or not.

    Hm i think i mixed it up with SnapIns

    > I guess in that case, having "-Value" as a non-mandatory parameter
    > doesn't seem to make sense...

    btw, it makes sense since you can declare a function with no body,
    although that won't do anything. It seems like -Value simply conforms to
    how function can be defined...
    [^_^]PS[106]>function test {}
    [^_^]PS[107]>test
    [^_^]PS[108]>


      My System SpecsSystem Spec

  6. #6


    dreeschkind Guest

    RE: Creating new functions using new-item cmdlet

    But then again, if functions don't need a value (function definition), then
    it should be possible to create new functions using the New-Item cmdlet
    without providing a value.

    --
    greetings
    dreeschkind

    "DBMwS" wrote:

    > > When you write a custom provider, as far as i remember,
    > > you can specify whether a parameter should be a mandatory one or not.

    > Hm i think i mixed it up with SnapIns
    >
    > > I guess in that case, having "-Value" as a non-mandatory parameter
    > > doesn't seem to make sense...

    > btw, it makes sense since you can declare a function with no body,
    > although that won't do anything. It seems like -Value simply conforms to
    > how function can be defined...
    > [^_^]PS[106]>function test {}
    > [^_^]PS[107]>test
    > [^_^]PS[108]>
    >


      My System SpecsSystem Spec


Creating new functions using new-item cmdlet
Similar Threads
Thread Forum
Problem creating private functions PowerShell
Creating objects with variable names in functions PowerShell
Creating cmdlet help PowerShell
Conflicting behaviour of rename-item cmdlet PowerShell
Parameter names for a New-Process Cmdlet: item name and working directory PowerShell