"new-item" isn't recognized as command in PowerShell - powershell

new-item -type file NAME/init.py
I have folder "NAME". The command "new-item" doesn't work. What should I do?

You should try :
New-Item -Path .\NAME -Name init.py -Type file
Use only the dot if you are located in your directory NAME, or enter the path (from the above directory or the full path).
ps: having the error output of powershell could be nice too.

This...
new-item -type file NAME/init.py
... is simply not valid syntax. See the PowerShell help files for details on the cmdlet and use. Be specific, don't guess or force PowerShell to guess for you.
For Example:
Get-ChildItem -Path 'D:/Temp' -Filter 'init.py'
# Results
<#
No results
#>
New-Item -Path 'D:\Temp' -Name 'init.py' -ItemType file -WhatIf
# Results
<#
What if: Performing the operation "Create File" on target "Destination: D:\Temp\init.py".
#>
New-Item -Path 'D:\Temp' -Name 'init.py' -ItemType file
# Results
<#
Directory: D:\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 18-Apr-21 20:45 0 init.py
#>
(Get-ChildItem -Path 'D:/Temp' -Filter 'init.py').FullName
# Results
<#
D:\Temp\init.py
#>
# Get specifics for a module, cmdlet, or function
(Get-Command -Name New-Item).Parameters
(Get-Command -Name New-Item).Parameters.Keys
Get-help -Name New-Item -Examples
# Results
<#
New-Item -Path .\TestFolder -ItemType Directory
New-Item -Path .\TestFolder\TestFile.txt -ItemType File
New-Item -Path .\TestFolder -ItemType Directory -Force
Get-ChildItem .\TestFolder\
New-Item ./TestFile.txt -ItemType File -Value 'This is just a test file'
#>
Get-Help -Name New-Item -Detailed
Get-help -Name New-Item -Full
Get-help -Name New-Item -Online

Related

Changing log directory script IIS

I ve tried to create a code which changes the directory from default to E:\Logfiles . Additionally I need to manage there some permissions groups which should be stored in XML or just added via ACL.
Could someone please give me advice how to manage with that?
My short code is below which should check if folder exist, go trough all servers on the farm.
Import-Module WebAdministration
$LogPath = “E:\LogFiles\”
foreach ($srv in (Get-SPServer | ? {($_.role -like "WebFrontEnd*") -or ($_.role -like "Application")}))
{
If(!(test-path $Logpath))
{
New-Item -ItemType Directory -Force -Path $path
}
foreach($site in (dir iis:\sites\*))
{
New-Item $LogPath\$($site.Name) -type directory
Set-ItemProperty IIS:\Sites\$($site.Name) -name logFile.directory -value “$LogPath\$($site.Name)”
}
}

Ceate folders for AD users using a CSV file produces wrong folder names

I have created a CSV file listing certain Active Directory users. Now I want to use this CSV to create a certain amount of folders for those users. I started with
$UserList = Import-Csv .\users.csv
and continued with
ForEach ($UserName in $UserList) {
$UserName
New-Item -Name $Username -ItemType directory -Path .\Download\$UserName
New-Item -Name $Username -ItemType directory -Path .\Home\$UserName
New-Item -Name $Username -ItemType directory -Path .\Publishing\$UserName
}
What I expect is that each folder Download, Home and Publishing contains a subfolder having the username, e.g. testuser.
When I run this script, the result for the folder names is #{name=testuser} which is not the expected result.
Any ideas how to solve this naming problem?
You need to dereference the property.
If you run this:
$UserList = Import-Csv .\users.csv
$UserList[0]
You should see something like this:
name
----
testuser
The name heading there tells you it's a property of the object $UserList.
For the solution, you could do this:
ForEach ($User in $UserList) {
$UserName = $User.Name
$UserName
New-Item -Name $Username -ItemType directory -Path .\Download\$UserName
New-Item -Name $Username -ItemType directory -Path .\Home\$UserName
New-Item -Name $Username -ItemType directory -Path .\Publishing\$UserName
}
Or this:
ForEach ($User in $UserList) {
$User.Name
New-Item -Name $Username -ItemType directory -Path .\Download\$($User.Name)
New-Item -Name $Username -ItemType directory -Path .\Home\$($User.Name)
New-Item -Name $Username -ItemType directory -Path .\Publishing\$($User.Name)
}
Or, alternately, you could get just the names on import like this:
$UserNameList = Import-Csv .\users.csv | Select-Object -ExpandProperty Name
ForEach ($UserName in $UserNameList) {
$UserName
New-Item -Name $Username -ItemType directory -Path .\Download\$UserName
New-Item -Name $Username -ItemType directory -Path .\Home\$UserName
New-Item -Name $Username -ItemType directory -Path .\Publishing\$UserName
}
However, if there are other values in your CSV file that you want to use, this last option isn't a good solution because you're only importing the name.

Copy Files on Same Directory using Powershell

I am trying to write powershell Script which will create backupfolder on same Path where Application exist and need to copy the folders & files into backupfolder before deploying. Below are the command was using to perform but am getting error
$Source = "C:\XYZ"
$BackupFolder = New-Item -ItemType Directory -Force -Path $source_$(Get-Date)
Copy-Item -Path $Source\* $BackupFolder -Force
Error: Cannot copy item C:\XYZ\Backup_18-02-2017 on to itself
Try:
Copy-Item $Source\* $BackupFolder -Exclude $BackupFolder
That will eliminate the folder that you are copying into as a source that is being copied from.
Variables can contain underscores. The following works and displays the string "asdf"
$a_ = "adsf"; $a_
Your New-Item cmdlet call should have failed since $source_ is not a variable and would return null. This is default behavior for PowerShell. When I run your code as is I get the following:
New-Item : Cannot find drive. A drive with the name '02/18/2017 22' does not exist.At line:1 char:1
+ New-Item -ItemType Directory -Force -Path "$source_$(Get-Date)" -what ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (02/18/2017 22:String) [New-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.NewItemCommand
So I would have expected your folder variable to be null. wOxxOm brings this up in comment as well
Several options to address what I am sure is the partial source of your issue.
$BackupFolder = New-Item -ItemType Directory -Force -Path "$source`_$(Get-Date)"
$BackupFolder = New-Item -ItemType Directory -Force -Path "$($source)_$(Get-Date)"
$BackupFolder = New-Item -ItemType Directory -Force -Path ("{0}_{1} -f "$source, Get-Date)
You will still have to try and exclude this folder from the copy as well like Keith Hill's answer is telling you
Copy-Item $Source\* $BackupFolder -Exclude $BackupFolder
try Something like this
$Source = "C:\XYZ"
$Destination="{0}{1:yyyyMMdd}" -f $source, (Get-Date)
New-Item -ItemType Directory -Force -Path $Destination
Copy-Item -Path $Source\* $Destination -Recurse -Force
If I understand the question correctly. You want to take "C:\XYZ" and backup into the same directory called "C:\XYZ\backup_$DATE". What you will actually do is create a loop that will break once it reaches the max 248 characters. If we use the -exclude option then we can exclude the backup directory "C:\XYZ\backup_$DATE".
This function will do the trick and also gives you error handling.
Function Get-CopyDirectory{
#####################
# Dynamic Variables #
#####################
$Date = Get-Date -format ddMM-yyyy
$Exclude="Backup*"
####################
# Static Variables #
####################
$AppPath = "F:\Test\"
$BackupPath = "$AppPath\BACKUP_$Date\"
if (Test-Path $BackupPath) {
Write-Host "Backup Exist" -f Cyan
}
else
{
Copy-Item "$AppPath\*" $BackupPath -Exclude $Exclude -recurse -verbose
}
}
CLS
Get-CopyDirectory

Set registry key to open notepad++

I tried running this script using the ISE, and I also tried to run it on the command line as administrator. It freezes at the "Remove-ItemProperty" line. I've tried to remove that step, but then it freezes at the next step "Set-ItemProperty". It looks like the New-Item lines are working fine.
if (Test-path "HKCR:\")
{
}
else
{
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
Unzip "npp.6.7.5.bin.zip" “C:\Notepad++”
New-Item -Type String "HKCR:\*\shell\Open With Notepad++"
New-Item -Type String "HKCR:\*\shell\Open With Notepad++\command"
Remove-ItemProperty "HKCR:\*\shell\Open With Notepad++\command" -name "(Default)"
Set-ItemProperty "HKCR:\*\shell\Open With Notepad++\command" -name "(Default)" -value "C:\\Notepad++\\notepad++.exe %1"
Any suggestions?
When using ItemProperty commands it interprets * as a wildcard. It's not freezing, it's searching every subkey of HKCR for "shell\Open With..." etc.
To force it to interpret the whole thing as a string path you need to use the -LiteralPath switch:
New-Item -Type String "HKCR:\*\shell\Open With Notepad++"
New-Item -Type String "HKCR:\*\shell\Open With Notepad++\command"
Set-ItemProperty -LiteralPath "HKCR:\*\shell\Open With Notepad++\command" -name "(Default)" -value "C:\\Notepad++\\notepad++.exe %1"

Piping values to New-Item to create directories in PowerShell

I've got a directory, C:\temp\test\, containing three DLL's I've called First.dll, Second.dll, Third.dll. I want to create sub-directories named after each of the DLL's.
This is what I've tried so far:
$dirName = "Tenth"
new-item $dirName -ItemType directory
That works. It created a sub-directory called "Tenth".
This also works:
(get-childitem -file).BaseName | select $_
It returns:
First
Second
Third
I've checked the type of the output from that command and it tells me "select $_" is of type System.String.
Now the bit that doesn't work:
(get-childitem -file).BaseName | new-item -Name $_ -ItemType directory
I get the following error repeated three times:
new-item : An item with the specified name C:\temp\test already exists.
At line:1 char:34
+ (get-childitem -file).BaseName | new-item -Name $_ -ItemType directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (C:\temp\test:String) [New-Item], IOException
+ FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.NewItemCommand
The current folder I'm executing the commands from is C:\temp\test\ .
I haven't been able to find any similar examples on the Internet to show me where I'm going wrong. Can anyone give me any pointers? Cheers.
Now the bit that doesn't work:
(get-childitem -file).BaseName | new-item -Name $_ -ItemType directory
This way, it works and doesn't need the ForEach-Object:
(dir -file).BaseName|ni -name{$_} -ItemType directory -WhatIf
$_ references each item as it comes along the pipeline so you will need to pipe to ForEach-Object for your line to work, like this:
(get-childitem -file).BaseName | ForEach-Object {new-item -Name $_ -ItemType directory}
This will create the item in the current powershell directory, you can also specify -Path if you want to create the folders somewhere else.
(get-childitem -file).BaseName | ForEach-Object {new-item -Name $_ -Path C:\MyFolder -ItemType directory}
New-Item accepts the parameter -Path to be piped and it can be an array of strings.
Then, you can create an object with a property Path that contains all the needed folders to be created
<#
# a simple array as example,
# but it could be the result of any enumerable,
# such as Get-ChildItem and stuff
#>
$FoldersToCreate = #('a', 'b', 'c')
# creates the folders a, b and c at the current working directory
[PSCustomObject]#{ Path = $FoldersToCreate } | New-Item -ItemType Directory
Or, alternatively :
$FoldersToCreate |
Select-Object #{ name = "Path"; expression = { "c:\testDir\$_" } } |
New-Item -ItemType Directory