1.    19 Jun 2006 #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.    19 Jun 2006 #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.    19 Jun 2006 #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.    19 Jun 2006 #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.    19 Jun 2006 #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.    19 Jun 2006 #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


Similar Threads
Thread Forum
Can the copy-item cmdlet be used to copy public folders to C: ?
Copying public folders via Outlook has a limitation on the number of items/emails that can be copied at once. When you have a large number of...
PowerShell
Creating a cmdlet using csc.exe - missing namespaces
I've got some C# code to create a custom cmdlet with these statements: .... using System.Collections; using System.Collections.Generic; using...
PowerShell
Problem creating private functions
I have a problem with private helper functions I did make within an other function (in TabExpansion function) I need to retrieve the available...
PowerShell
Creating objects with variable names in functions
Hi, I am sure there is a better way but I would like to create objects with names that are formed in a function that are accessable from the main...
PowerShell
Creating cmdlet help
I've started developing cmdlets and the associated help file. The cmdlet help file is in a specific XML format. Manually creating these XML help...
PowerShell
Conflicting behaviour of rename-item cmdlet
Hi there, I was just writing a quick PoSH script to convert the names of all files and directories at a specified location recursively to...
PowerShell
Parameter names for a New-Process Cmdlet: item name and working directory
The New-Process cmdlet I'm working on has a couple of parameters that I find awkward to name correctly using the PowerShell guidance; the parameters...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • PC Help Forum
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 06:49.
    .