Announcement

Collapse
No announcement yet.

Convert a powershell command line into a script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Convert a powershell command line into a script

    wow.. first post here.. i'll post about myself in the welcome board shortly.

    I have a command line that I use to ensure that all users in a particular OU remain enabled. I want to convert it to a PowerShell script but haven't a clue. I really need to take some lessons.. here's the command if I can get some help would be awesome.

    Get-ADUser -Filter 'Name -like "*"' -SearchBase "OU=test,DC=mydomain,DC=local" | Enable-ADAccount

    This works well as it leaves the accounts that are already enabled alone.

    just don't know the code well enough to make this a .ps1 so I can set a schedule task.

  • #2
    Re: Convert a powershell command line into a script

    Originally posted by Dr1nkbeer View Post
    wow.. first post here.. i'll post about myself in the welcome board shortly.

    I have a command line that I use to ensure that all users in a particular OU remain enabled. I want to convert it to a PowerShell script but haven't a clue. I really need to take some lessons.. here's the command if I can get some help would be awesome.

    Get-ADUser -Filter 'Name -like "*"' -SearchBase "OU=test,DC=mydomain,DC=local" | Enable-ADAccount

    This works well as it leaves the accounts that are already enabled alone.

    just don't know the code well enough to make this a .ps1 so I can set a schedule task.
    It should simply be a case of copying the command

    Code:
    Get-ADUser -Filter 'Name -like "*"' -SearchBase "OU=test,DC=mydomain,DC=local" | Enable-ADAccount
    into a notepad file and save it with a .ps1 extension.

    To run it you would open powershell and browse to the directory and type .\scriptname.ps1 and it should run. It will run if you have the correct execution policy set.

    Comment


    • #3
      Re: Convert a powershell command line into a script

      Originally posted by Dr1nkbeer View Post
      ...so I can set a scheduled task.
      To expand upon Wullieb1's excellent post, the best way that I have found to schedule a PowerShell script in task scheduler (in Win7 or Server 2k8 R2, at least) is to put the following path in the "Program/script" field:

      Code:
      %windir%\system32\WindowsPowerShell\v1.0\powershell.exe

      ...then place the following in the "Add arguments (optional)" field, replacing the dummy path with the path to, and name of, your PS1 file:

      Code:
      -noprofile –noninteractive -command  ". 'C:\SCRIPTS\Example_Script.ps1

      Hope this helps!


      Comment

      Working...
      X