Results 1 to 5 of 5

Thread: Create a script using Powershell

  1. #1
    Join Date
    Nov 2008
    Posts
    1,066

    Create a script using Powershell

    I am completely new to Powershell. I want to create a script which will output the disk usage for my hard drives to a single text file and also the CPU and memory usage.

    I have no idea from where to start with the Powershell, it all looks really complicated! Can someone shed light on this or explain me how to proceed?

  2. #2
    Join Date
    May 2008
    Posts
    271

    Re: Create a script using Powershell

    For security reason MS has not associated ps1 extension with powershell instead it is associated with notepad. You may change that by making registry changes. In addition, by default the script won't be executed unless you issue command

    Set-ExecutionPolicy Unrestricted

    or sign your scripts. Read more about this by typing

    get-help about_signing

    in PowerShell.

  3. #3
    Join Date
    May 2008
    Posts
    685

    Re: Create a script using Powershell

    Script Cmdlets are one of the coolest things about the newer version of PowerShell. A Script cmdlet allows you to use all of the variety of cmdlet parameter sets inside of PowerShell functions.

    Since Script Cmdlets are PowerShell functions, and the PowerShell engine prefers to run functions rather than commands, you can use Script Cmdlets to override an existing cmdlet. You might want to do this to add or remove parameters from a cmdlet you use often. Also, you might just want to see what a cmdlet’s parameters look like in a script cmdlet, so you can go write your own.

    Luckily, Powershell has a way to generate script cmdlets from an existing cmdlet. Below is a script cmdlet, called New-ScriptCmdlet, that I’ll use to create other script cmdlets.

    You can use this to create new script cmdlets from existing cmdlets in a couple of different ways:

    # Create a new PowerShell cmdlet from an existing cmdlet

    Get-Command Get-Command | New-ScriptCmdlet Get-Command | Set-Content Get-Command.ps1

    # Create a new PowerShell cmdlet from an existing cmdlet’s type

    [Microsoft.PowerShell.Commands.GetProcessCommand] | New-ScriptCmdlet Get-Process | Set-Content Get-Process.ps1

    # Creates a new PowerShell cmdlet from a random existing command and puts it into a file of the same name

    Get-Command | Get-Random | Foreach-Object {
    $cmdlet = $_
    $scriptCmdlet = $cmdlet | New-ScriptCmdlet $cmdlet.Name
    $scriptCmdlet | Set-Content "$($cmdlet.Name).ps1"
    }

    Hope this helps,

  4. #4
    Join Date
    May 2008
    Posts
    945

    Re: Create a script using Powershell

    Here is a guide from Microsoft to let you start with Windows PowerShell. Read through these pages to get familiar with Windows PowerShell, and soon you’ll be driving around like a pro.

    http://www.microsoft.com/technet/scr...anual/run.mspx

  5. #5
    Join Date
    Feb 2008
    Posts
    194

    Re: Create a script using Powershell

    PowerShell Cmdlets (Command Lets)

    First Meaning of PowerShell Cmdlet
    Cmdlet has two meanings in Windows PowerShell; the first meaning of cmdlet is a synonym for a PowerShell script. A PowerShell cmdlet is a series of commands, usually more than one line, stored in a text file with a .ps1 extension. It is this scriplet meaning of cmdlet that I feature on this page.

    Second Meaning of PowerShell Cmdlet
    In PowerShell circles there appears to be a second meaning for the word cmdlet. For example Microsoft say, cmdlet means a built-in PowerShell command, which corresponds to a single verb-noun pair. Such a cmdlet is often accompanied by an alias, for example: get-Member with its alias of gm.

    Advantages of Cmdlets
    Once you have experimented with a few basic instructions at the PowerShell command line, you may wish to store your code in its own cmdlet file. The benefit is that you can replay these perfect lines of instructions later. This strategy becomes more and more useful as the code grow in complexity. My tactic is once a cmdlet achieves my basic objective, I call for 'SaveAs', and then use the copy for further development. Seven times out of ten the 'development' goes hopelessly wrong, at which point I am so grateful that I saved a working copy.

    I hope that you are getting the idea of a cmdlet. Spend time perfecting the PowerShell commands, then save them in a text file. This technique saves you typing in lines of code and the command line, instead, you call the cmdlet by typing: dot slash and followed by its filename, for example .\memory. Incidentally, building PowerShell cmdlets fits in with my strategy of assembling scripts in sections.

Similar Threads

  1. Powershell Script to Enable/Disable Network Connection
    By windows_user in forum Software Development
    Replies: 8
    Last Post: 27-04-2012, 09:35 PM
  2. Running Powershell script with batch file with parameters?
    By Jaslein in forum Software Development
    Replies: 3
    Last Post: 12-01-2011, 03:36 AM
  3. How to create Script for Always-on-top message?
    By pushpendra in forum Software Development
    Replies: 4
    Last Post: 06-02-2010, 06:30 AM
  4. Powershell script to parse system logs in text file
    By SADIQ in forum Operating Systems
    Replies: 2
    Last Post: 02-06-2009, 07:05 PM
  5. How to display powershell script output to XML
    By StephanieRice in forum Software Development
    Replies: 3
    Last Post: 01-06-2009, 02:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,391,955.27881 seconds with 16 queries